Comments 0 197 Views

# Python program to check whether a number or string is palindrome or not

A number or string is said to be a palindrome if its reverse is the same as the given number or string. For example – Malayalam is a palindrome string and 121 is a palindrome number whereas 120 and Tamil are not palindromes.

In this article, we will write a program in Python that will check if a user-entered number or string is palindrome or not.

## Python program to check if a number is palindrome or not

We will use the Python while loop to iterate and reverse the number and then compare it with the given number that if these are equal or not if these are equal that means the number is palindrome otherwise it is not.

Now see the source code –

```n=int(input("Enter a number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is palindrome!")
else:
print("Given number is not a palindrome!")```

Given image shows how this program asks the user to enter a number and check if it is palindrome or not. ## Python program to check if a string is palindrome or not

To reverse a string we will use -1 step argument and compare it with the given string if these are equal that means the string is palindrome otherwise it is not.

See the source code below –

```str=input(("Enter a string:"))
if(str==str[::-1]):
print("The string is a palindrome")
else:
print("Given string is not a palindrome")```

Now see the image below which shows the output of the program when a user enters a string – There are many other ways by which you can implement this palindrome program for example you can use methods like reversed() to reverse a string.

Now if you want to say something about this then write us in the comments below.

#### About author #### Introduction to Python functions

This post was last updated on March 6th, 2021 at 08:20 amA Python function is a self-contained module of code that performs a specific task. It takes inputs, processes them,

#### Python List vs Array

Like in many other programming languages Python also provides some inbuilt data types this includes list, tuple, sets, dictionaries, etc. In this article, we will discuss the similarities and differences

#### Comments In Python

The comments in a programming language are used to explain something in the source code. For example, it can be used to explain what a line of code or block

#### No Comments Yet!

You can be first to comment this post!