Palindrome program in python language

Here is a simple Python program that checks whether a given word is a palindrome or not:

word = input("Enter a word: ")

# Reverse the input word
reverse_word = word[::-1]

# Compare the original word with the reversed word
if word == reverse_word:
    print(word, "is a palindrome")
else:
    print(word, "is not a palindrome")

Here’s how it works:

  1. First, we ask the user to enter a word using the input function.
  2. We then use slicing with a step of -1 to reverse the word and store the reversed word in the reverse_word variable.
  3. Finally, we compare the original word with the reversed word using an if statement. If they are the same, we print that the word is a palindrome, otherwise we print that it’s not a palindrome.

Note that this program assumes that the input word contains only lowercase letters. If you want to handle uppercase letters as well, you can convert the input word and the reversed word to lowercase using the lower function before comparing them.

What is a palindrome?:

A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. In other words, if you reverse the order of the characters, you get the same sequence.

Examples of palindromes include:

  • racecar
  • level
  • madam
  • deed
  • 1221

Palindromes can be found in various languages and can include special characters, spaces, and punctuation. Palindromes are often used in literature, puzzles, and word games. They can also be used as a test of wordplay and logic skills.

Palindrome string program:

Here’s a simple Python program that checks whether a given string is a palindrome or not:

def is_palindrome(s):
    """
    This function checks whether a given string is a palindrome or not.
    """
    s = s.lower()   # Convert string to lowercase
    s = s.replace(" ", "")   # Remove all spaces from string
    return s == s[::-1]   # Compare string with its reverse

# Test the function with some examples
print(is_palindrome("racecar"))   # Output: True
print(is_palindrome("A man a plan a canal Panama"))   # Output: True
print(is_palindrome("hello"))   # Output: False

Here’s how the program works:

  1. We define a function called is_palindrome that takes a string as its argument.
  2. The function first converts the string to lowercase using the lower() method. This is done to ensure that the function is case-insensitive and can handle strings with both uppercase and lowercase letters.
  3. The function then reverses the string using slicing and stores the result in a new variable called reversed_string.
  4. Finally, the function returns True if the original string is equal to the reversed string, indicating that the string is a palindrome, and returns False otherwise.

In the example usage, we first ask the user to enter a string using the input function. We then call the is_palindrome function with the user input as its argument, and print the appropriate message depending on the function’s return value.

Palindrome number program using while loop:

Sure, here’s a simple Python program that checks whether a given number is a palindrome or not using a while loop:

def is_palindrome(number):
    """
    Checks whether the given number is a palindrome or not.
    """
    original_number = number  # Store the original number
    reverse_number = 0  # Initialize the reverse number to zero
    while number > 0:
        remainder = number % 10  # Get the last digit of the number
        reverse_number = reverse_number * 10 + remainder  # Add the digit to the reverse number
        number //= 10  # Remove the last digit from the number
    return original_number == reverse_number


# Example usage
number = int(input("Enter a number: "))
if is_palindrome(number):
    print(number, "is a palindrome")
else:
    print(number, "is not a palindrome")

Here’s how the program works:

  1. We define a function called is_palindrome that takes a number as its argument.
  2. The function first stores the original number in a variable called original_number.
  3. The function initializes a variable called reverse_number to zero.
  4. The function then enters a while loop that runs as long as the number is greater than zero.
  5. Inside the loop, the function gets the last digit of the number using the modulo operator and stores it in a variable called remainder.
  6. The function then adds the digit to the reverse number by multiplying the reverse number by 10 and adding the remainder.
  7. The function removes the last digit from the number by using integer division to divide the number by 10.
  8. The loop repeats until the number becomes zero, at which point the function compares the original number with the reverse number and returns True if they are equal, indicating that the number is a palindrome, and returns False otherwise.

In the example usage, we first ask the user to enter a number using the input function and convert it to an integer using the int function. We then call the is_palindrome function with the user input as its argument, and print the appropriate message depending on the function’s return value.