How To Find Palindrome Number and String In Python

In many interview questions, you may ask to create a program to find palindrome number or string using Python. Here are the explanation and examples you can use check if the number or string is palindrome or not.

What is Palindrome Number and String?

A palindrome is a number or string which remains the same when you reverse it. You will get the same number and string after you reverse each digit of 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 gives you the same string after reverse.

Find Palindrome Number Using Python

If you want to check the given number is palindrome or not. You have to use the %(modulo) operator of Python and use the below method. The below method get the remainder of the number and store in the variable. The reverse variable then store the reversed digits of the number. You may also like to read Check If The Number Is Even Or Odd In Python.

In the last step of this method, it stores the reversed digits of the number in the 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.

Output

The reverse of num is: 1991
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 String is a Palindrome with Python

In addition to the above method, you can also find the string whether it is palindrome or not. You can use the below given two methods to find your palindrome string using Python.

Method 1: 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.

Output

The reverse of string is: refer
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.

Method 2: 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.

Output

The given string is Palindrome.

The above example also checks the palindrome string with very less 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

Hope, you like the post of how to find the palindrome number and string using Python. If you have any query regarding the tutorial, please comment below.

Also tell me, which method you are using to find your palindrome number and string using Python.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.