Python Program to Check Whether a String is Palindrome or Not
A palindrome is a word, phrase, number, or sequence of characters that reads the same backward as forward. In other words, it is a sequence of characters that remains the same even when the order of its characters is reversed.
Examples of palindromic words include “racecar”, “level”, “deified”, “radar”, and “noon”.
Python Code :
The below Python program checks whether a string is palindrome or not:
# Take input string from user string = input("Enter a string: ") # Remove all spaces and convert to lowercase string = string.replace(" ", "").lower() # Check if the string is equal to its reverse if string == string[::-1]: print("The string is a palindrome") else: print("The string is not a palindrome")
The program takes a string input from the user and stores it in the variable string.
The replace() method is used to remove all spaces from the string, and the lower() method is used to convert it to lowercase.
The [::-1] syntax is used to reverse the string.
The program checks if the original string is equal to its reverse using an if statement. If they are equal, the program prints that the string is a palindrome. Otherwise, it prints that the string is not a palindrome.
Enter a string: elonmusk The string is not a palindrome Enter a string: ABACABA The string is a palindrome