We always seemed to find it interesting to read strings backwards; eventually, we learned that such strings, which can be read correctly either way, are known as Palindromes. Although we enjoy other languages, none of them are as well-known as Python because of our keen interest in teaching our machines to recognise Palindromes in Python.
Checking to determine if a given string is a Palindrome in Python is one of the most common programs you will run as a novice as you practice your coding abilities in Python language. This article describes a few different methods for checking Palindrome programs in Python.
1What does Palindrome mean?
A number or word that is unchanged when its digits or letters are reversed is known as a palindrome. As we can see, palindromes are utilised frequently in everyday life.
- Some examples of palindromes are the numbers 232, 77, 313, 5665, and 84847.
- The words BOB, DAD, MADAM, and REFER are all palindromes.
- The words in SLAJOBS, POWER, and JAVA are not palindromes.
2Algorithm for Palindromes
What comes to mind when you are asked to determine whether a particular number is a palindrome? The reverse of the number is found, and then it is compared to the original number to determine if they are the same, correct? The technique used to check Palindromes in Python takes the same approach. The steps are as follows:
Step 1: Enter the given input
Step 2: Determine the reverse of the given input
Step 3: Check if the reversed number generated in Step 2 is the same as the given input. If so, print “Palindrome” and go to step 5; otherwise, go to step 4.
Step 4: Print “Not a Palindrome”
Step 5: End
3Palindrome in Python
With the aforementioned steps, the Palindrome program in Python would look like:
Palindrome in Python: Example
str = ‘Level’
str str = str.casefold()
# This string is reverse.
rev = reversed(str)
if list(str) == list(rev):
print(“PALINDROME !”)
else:
print(“NOT PALINDROME !”)
Output
Palindrome!
There are various methods to determine Palindromes in Python. Some of them are:
3.1Palindrome in Python: Using for loop
The for loop is used in this method to iterate Palindromes in Python.
Program:
Output:
Enter string: NOON
Reversed string: NOON
“The String is a Palindrome”
3.2Palindrome in Python: using while Loop
While loop is utilized in this method to iterate Palindromes in Python. This is one of the simplest method to find palindromes when using a while loop in Python programming.
Program:
number=int(input(“Enter any number :”))
#store a copy of this number
temp=number
#calculate reverse of this number
reverse_num=0
while(number>0):
#extract last digit of this number
digit=number%10
#append this digit in reveresed number
reverse_num=reverse_num*10+digit
#floor divide the number omit the number’s last digit
number=number//10
#compare reverse to original number
if(temp==reverse_num):
print(“The number is Palindrome!”)
else:
print(“Not a Palindrome!”)
Output:
Enter the value: 2586
Not a Palindrome!
Enter the value: 4884
The number is Palindrome!
3.3Palindrome in Python: Built-in Functions
In addition to using loops in python, a function can also be used to determine Palindromes in Python. In this method, built-in reversed() is used to iterate Palindromes in Python.
4Conclusion
Palindromes are extremely interesting to practise, and the addition of the popular programming language Python makes them even more exciting. The methods described above can be used to check Palindromes in Python.