Palindrome Program in Python

Following Python code will check if a string is palindrome or not.


  1. Define string to check for palindrome.
  2. Create a function that will take input string for checking.
  3. In the function, first reverse the string and then check by comparison whether original string and the reversed string is the same.
  4. If both the strings are same then print “It is palindrome” else print “It is not palindrome”.

How Does Palindrome Program Works?

A word, phrase, or sequence is said to be palindrome if, it reads the same backwards as forwards, e.g. “madam” or “nurses run”. A number is also a palindrome number if it reads the same backwards or forwards, e.g. “16461”.

To check for a palindrome string, a function is defined that will take a string as an input. The inputted string is then reversed, that is, it is stored in backwards spelling as the reversed string. After that, list() is used to list the characters in a string. Both the input string and the reversed string are converted to lowercase then list() is used to check if the sequence of characters are same in both the string. If yes, then a message will be printed saying “It is palindrome” else a message will be printed saying “It is not palindrome”.

Python Program To Check if a string is Palindrome or not:

# change this value for a different output
str = 'aIbohPhoBiA'

def palindrome_or_not(str):
rev_str = reversed(str.lower())
if list(str.lower()) == list(rev_str):
print("It is palindrome")
print("It is not palindrome")


Output for the above Program execution is:
It is palindrome

Instead of hard coding the values, the code can also be modified to get input from the user during program execution. Refer here to know how to get user input from Python code execution.

Leave a Reply