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.
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.
Leave a Reply