Python Program to Check Whether a String is Palindrome or Not

In this program. you'll learn to check whether a string is palindrome or Not

To understand this example, you should have the knowledge of following Python programming topics:

A palindrome is a string which is same read forward or backwards.

For example: "dad" is the same in forward or reverse direction. Another example is "aibohphobia" which literally means, an irritable fear of palindromes.

Source Code

# Program to check if a string
#  is palindrome or not

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

# make it suitable for caseless comparison
my_str = my_str.casefold()

# reverse the string
rev_str = reversed(my_str)

# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
   print("It is palindrome")
else:
   print("It is not palindrome")

Output

It is palindrome

Note: To test the program, change the value of my_str in the program.

In this program, we have taken a string stored in my_str.

Using the method casefold() we make it suitable for caseless comparisons. Basically, this method returns a lowercased version of the string.

We reverse the string using the built-in function reversed(). Since this function returns a reversed object, we use the list() function to convert them into a list before comparing.