Given a string as an input to the Python program you have to reverse the string and compare it with the original string to check if the string is palindrome or not.
A string is said to be a palindrome if you reverse the string and the original string are same, then the string is said to be a palindrome.
Write a Program to Check Whether a String is Palindrome or Not in Python
Let's take a variable named as text and ask a user to enter the string using the input function call and then convert it into lowercase letters using the inbuilt function lower().
Let's convert the input string to reverse using the string slicing technique that is [::-1] it will reverse the original string. store the reverse string into another variable.
text = input("Enter a String : ")
text = text.lower()
reverse_string = text[::-1]
Using if statement Compare original string with the reverse string check if both are same if so print the string is a palindrome using the else part print the string is not a palindrome.
if text == reverse_string:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
Python complete working code
===============================
text = input("Enter a String : ")
text = text.lower()
reverse_string = text[::-1]
if text == reverse_string:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
Output 1
====================
Enter a String : madam
The string is a palindrome.
Output 2
=====================
Enter a String : Monday
The string is not a palindrome.
The above Python program can also be implemented using the for loop as shown below
As usual, take a variable named after him and ask a user to enter a string and then convert the string to lowercase.
text = input("Enter a String : ")
text = text.lower()
Initialize index to -1 then using the following iterate all the letters present in the string and check if the letter is not equals to the letter present in index.
Letters are not the same then print the string is not a palindrome and break the for loop. using the else part decrements the index by 1. using the following else part once the for loop is completed the comparison. Use else part 2 to print the string is palindrome.
index = -1
for letter in text:
if letter != text[index]:
print("The string is not a palindrome.")
break
else:
index -= 1
else:
print("The string is a palindrome.")
Complete Python working code using for loop
============================================
text = input("Enter a String : ")
text = text.lower()
index = -1
for letter in text:
if letter != text[index]:
print("The string is not a palindrome.")
break
else:
index -= 1
else:
print("The string is a palindrome.")
Output
===================
Enter a String : avatar 2021
The string is not a palindrome.
Output 2
====================
Enter a String : malayalam
The string is a palindrome.
Conclusion
==========================
Execute the above program by providing the random palindrome string and on the results.
Comment in down below if you have any queries or suggestion about program
0 Comments