How to Check Whether a String or Number is a Palindrome or not Using Python

In this post, we will learn "How to Check Whether a String or Number is a Palindrome or not Using Python".

What is a Palindrome?

A palindrome is a string or number which is when read from starting or end it will spell the same.

For example:- radar, it will spell same whether we read it from start or end. There are many more like dad, Malayalam etc.

We will write the python program to check for both number and string.

To check a number or string is palindrome or not, we have four ways:-

  • Checking a number is palindrome or not
  • Checking a string is palindrome or not
    1. Using slice
    2. Using an inbuilt function.
    3. Iterative method.

Let's discuss all the ways one by one.


Checking a number is palindrome or not

In this method, we will check for a number is palindrome or not. Here we will first find the reverse of the number and store in the rnum variable. Later we will check whether the given number is same as the reverse number.

Code:-

# Taking a number from user
num = int(input("Enter a number: "))
# initializing variable rnum to 0. It is used to store the reverse of num
rnum = 0
# initializing temp variable to num
temp = num
# Reversing the num and storing in rnum variable using while loop
while temp > 0:
    rnum = rnum*10 + (temp%10)
    temp = temp//10

# Checking whether the given string and reversed string is same
if num == rnum:
    print(num,"is a palindrome.")
else:
    print(num,"is not a palindrome.")

Output:-

Enter a number: 1568651
1568651 is a palindrome.



Checking a string is palindrome or not

Using the slice

Here we have used the slice technique. Using split we can get the individual characters of the given string and store in another string variable.

Code:-

# Taking a string from user
s = input("Enter a string: ")
# Reversing the string s and storing in rs variable using slice
rs = s[len(s)::-1]
# Checking whether the given string and reversed string is same
if s == rs:
    print("The string is a palindrome.")
else:
    print("The string is not a palindrome.")

Output:-

Enter a string: radar
The string is a palindrome.



Using an inbuilt function

In this, we will use the reversed() inbuilt function, which will reverse the characters of the string and returns a reversed object. So we have to convert this object to a string so that we can compare it with the given string.

Hence we have used the join() function which will convert this object to a string and store it in rs variable.

Code:-

# Taking a string from user
s = input("Enter a string: ")
# Reversing the string s using in-build function
rs = ''.join(reversed(s))
# Checking whether the given string and reversed string is same
if s == rs:
    print("The string is a palindrome.")
else:
    print("The string is not a palindrome.")

Output:-

Enter a string: malayalam
The string is a palindrome.



Using an iterative method

In this method, we have used the for loop which will start from the first character of the string till the half of the string and check whether the characters present in the first half is same as the other half or not.

If any character is not found to be same then found variable is set to False and break statement is called.

And if all the characters are same in both the halves then the else statement will be executed, which is found = True.

Code:-

# Taking a string from user
s = input("Enter a string: ")

# checking for palindrome using the iterative method(for loop)
for i in range(0,len(s)//2):
    if(s[i] != s[len(s)-i-1]):
        found = False
        break
else:
    found = True

# Checking whether the given string and reversed string is same
if found:
    print("The string is a palindrome.")
else:
    print("The string is not a palindrome.")

Output:-

Enter a string: aibohphobia
The string is a palindrome.

Hence we have discussed all the ways. If you find any problem then you can comment below.

I have also discussed all the techniques to reverse the number and string.

You can learn about many other Python Programs Here.







The following two tabs change content below.
Amit Rawat

Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.

You may also like...