Last Updated on May 16, 2024 by Roshan Parihar
To check if a number is a palindrome in Python, you have to check whether its reverse is the same or not. If the reverse of the number or string are same then it is a palindrome number or string.
In many interview questions, you may ask to create a program to find a palindrome number or string using Python. Here are use examples with explanations to help you find out if the number or string is palindrome or not.
What is Palindrome Number or String?
A palindrome is a number or string which remains the same when you reverse it. You will get the same number or string after you reverse each digit of the number or string characters.
There are many palindrome numbers with all single-digit numbers. Two-digit numbers like 11, 22, …99 are also the palindrome number. The same you can say with the three digits and other digits numbers.
The same will be applied to the palindrome string. There are many strings which are a palindrome in nature. The strings like level, pop, etc are some palindrome strings. These strings if you reverse will give you the same string after reverse.
How to Check if the Number is a Palindrome Number in Python?
If you want to check whether the given number is palindrome or not, you have to use the %(modulo) operator in Python. In the below example get the remainder of the number and store it in the variable. The reverse variable then stores the reversed digits of the number in a new variable.
After that, it checks whether the reversed number matches with the given number or not. If the number matches with the given number, it confirms that the given number is Palindrome.
Example 1: Check if a number is Palindrome in Python
1 2 3 4 5 6 7 8 9 10 11 12 |
num = 1991 tempNum = num rev = 0; while tempNum > 0: numDig = tempNum % 10 rev = rev * 10 + numDig tempNum = tempNum // 10 print("The reverse of num is:", rev) if rev == num: print("The given number is Palindrome.") else: print("The given number is not Palindrome.") |
Output
The given number is Palindrome.
The above example shows that the above-given number is palindrome using the above method. You can change the above example as per your requirement. There are many other numbers available which are a palindrome. Find your own palindrome number with the above method.
Check if a Given String is a Palindrome String in Python
In addition to the above method, you can also check whether the string is palindrome string or not. You can use the examples to find your palindrome string using Python.
Find String if it is Palindrome
This method uses the while loop of Python to reverse the string and check if it is palindrome or not.
Example 2: Check if a string is Palindrome or not
1 2 3 4 5 6 7 8 9 10 11 12 13 |
myStr = "refer" j = myStr rev = "" while len(j) > 0: if len(j) > 0: a = j[-1] j = j[:-1] rev = rev + a print("The reverse of string is:", rev) if rev == myStr: print("The given string is Palindrome.") else: print("The given string is not Palindrome.") |
Output
The given string is Palindrome.
The above example shows that the above-given string is a palindrome. You can also check your string if it is palindrome using the below method.
Check The Palindrome String with Less Code in Python
The second method requires less code to write and check palindrome. You have to use the below-given method to check your string if the string is palindrome or not using Python.
Example 3: Check if a string is Palindrome or not
1 2 3 4 5 |
myStr = "level" if str(myStr) == str(myStr)[::-1]: print("The given string is Palindrome.") else: print("The given string is not Palindrome.") |
Output
The above example also checks the palindrome string with very little code to write. It does not store or reverse the string but checks with the single line code for palindrome.
You may also like to read