Skip to content

How To Check Whether A String Is Palindrome Or Not In Python?

In this article, you will learn how to check whether a string is palindrome or not using python programming language. Enroll for our Python course and learn everything with help of professional instructors. 

If you say a string is a palindrome, it means the string has the same meaning even after reversing the string. There are several string palindromes which you can use to check whether their meaning changes after reversing the string or not.

Palindrome Examples:

Madam

Noon

Civic

Malayalam

Racecar

Rotator

Ailihphilia

Level

Wow

Radar, just to name a few

Steps involved to check whether a string is palindrome or not

If you want to check whether a string is a palindrome or not using Python programming language, you just have to follow these four easy steps.

Step 1st: Finding the reverse of the given string using function.

Step 2nd: Calling the function.

Step 3rd: Comparing the reversed string with the original string to check whether they have the same meaning or not.

Step 4th: Printing the result.

Python Program To Check Whether A String Is Palindrome Or Not

Example 1:
# This is a python program to illustrate whether a string
# is palindrome or not
def reverse(string): 
  return string[::-1]
def isPalindrome(string): 
  r = reverse(string) 
  if (string == r):      
 return True  
 return False
string = “racecar”
result = isPalindrome(string)
if result == 1:   
print(“It is a palindrome”)
else:   
print(“It is not a palindrome”)
Output 1:
It is a palindrome

Cleary, the string ‘racecar’ is a palindrome. Now let’s perform this test with another string.

Example 2:
# This is another python program to illustrate whether a string
# is palindrome or not
def reverse(string):  
 return string[::-1]
def isPalindrome(string): 
  r = reverse(string) 
  if (string == r):      
 return True  
 return False
string = “palindrome”
result = isPalindrome(string)
if result == 1:   
print(“It is a palindrome”)
else:   
print(“It is not a palindrome”)
Output 2:
It is not a palindrome

You can also perform this test by using a built-in string function reversed() which returns a reversed object. Before comparing the reversed object with the original string, you need to convert it into a list.

Example 3: When all the letters of a string are already lowercased
# This is a python program to check whether a string
# is palindrome or not using built-in
# string function reversed()
my_string = ‘level’
reversed_string = reversed(my_string)
if list(my_string) == list(reversed_string): 
 print(“The given string is a palindrome”)
else:  
print(“The given string is not a palindrome”)
Output 3:
The given string is a palindrome

In the above example, all the letters of the given string is already in lowercase. Therefore, the program is successfully executed.

But, if the string has both the uppercase and lowercase letters, you will first need to convert all the letters of the string either into lowercase or uppercase by using lower() and upper() method respectively. Let’s have a look on the below example.

Example 4: When a string has both uppercase and lowercase letters
# This is a python program to check whether a
# string(having both uppercase and lowercase letters)
# is palindrome or not using built-in
# string function reversed()
my_string = ‘LeVel’
my_string = my_string.upper()
reversed_string = reversed(my_string)
if list(my_string) == list(reversed_string): 
 print(“The given string is a palindrome”)
else: 
 print(“The given string is not a palindrome”)
Output 4:
The given string is a palindrome

We have briefly described how to check whether a given string is palindrome or not with and without using functions by applying different methods in python programming language. Hope you like this article.

Facebook
Twitter
LinkedIn
Pinterest

Online Digital Marketing Course with 5 Days Free Classes.

Are you one of them who think Online classes are not practical and Interactive.

Start with 5 Days Free Classes, to experience our quality of training Before Enrollment.