Python Examples

Python Program to Check Whether a String is Palindrome or Not 

Source Code 

A palindrome is a word or a sentence that reads the same whether read from left to right or right to left. The palindrome Python program will check whether the string that is entered by the user is the same when read forward or backwards. 

palindrome python

Source

Thus, to understand how the program works, you will need to understand how string works and various string methods that are available in the Python library. 

As a specific condition needs to be checked, we will also require the if…else statement in the program. The palindrome Python source code is provided here. 

                    

str_1 = input (“Enter the string to check if it is a palindrome: “)

str_1 = str_1.casefold ()

rev_str = reversed (str_1)

if (list str_1) == list (rev_str):

              print (“The string is a palindrome.”)

else:

              print (“The string is not a palindrome.”)

Here in the code, the first step is to ask the user to enter a string that they want to check if it is a palindrome or not. Next, the casefold () method is used to ensure the string is caseless. Thus, the character for capital A will be treated the same as a character of lowercase a. The casefold () method is used to return a string that is passed in the parameters in its lowercase version.  

Once the string is converted into lowercase, it will then be reversed from backwards to forward with the method reversed (). It is a built-in method that is available for strings in Python. to compare the original string with the reversed string, both will be converted into list form. 

The list () function is used to convert the strings into list form. Then, the equal to, that is, the ‘==’ operator is used to compare elements of both the lists. 

If all the elements present in the lists are equal, then the program will print that the string provided by the user is a palindrome. If not, then the code will print that the string is not a palindrome. 

Alternative Method:

Another method that does not use the in-built string methods available in the Python library is provided below. The algorithm for the source code for this palindrome Python program is that it checks the characters of the string from half the length. 

                    

def check_palin (str):

              for i in range (0, int (len (str)/2)):

                             if str [i] != str [len (str) -i-1]:

                                            return False 

                             return True

str_1 = input (“Enter a string.”)

ans = check_palin (str_1)

if (ans):

              print (“Yes, it is a palindrome.”)

else:

              print (“No, it is not a palindrome.”)

The program uses the len () function to obtain the length of the string and then halves it. It then uses the FOR loop to iterate over each element of the string from start to half to check if it is equal to the second half of the string. 

Frequently Asked Questions 

What is a palindrome in Python?

A palindrome Python program is used to check whether a string is the same from forward to backward. 

How do you write a palindrome in Python?

To write a Palindrome Python program, one can use the reversed () method that is available in the Python library. 

How do you check whether a number is a palindrome or not in Python?

Using the ‘==’ or equal to operator, the reverse and original string is compared. 

Where is the closest palindrome in Python? 

To find the closest palindrome in Python, check which character of the string is different and then change it accordingly. 

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.