Mastering Python (71 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (30 Blogs)
  • Data Science (39 Blogs)
  • Mastering Python (66 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Palindrome in Python: How to check a number is palindrome?

Published on Aug 15,2019 1.9K Views
Aayushi Johari
A technophile who likes writing about different technologies and spreading knowledge. A technophile who likes writing about different technologies and spreading knowledge.
1 / 11 Blog from Python Programs

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

As kids, it was fun reading reverse strings and when we grew up a little we learned that strings reading same either ways are called palindromes. Curiosity dint leaves us there, so we wanted our machines to learn what are palindromes and for all Python lovers, no other language can do it in a better way. If you are a python lover and a coding enthusiast, read along to learn how to create a Palindrome in Python.

Let’s begin.

What is a Palindrome?

A palindrome is nothing but any number or a string which remains unaltered when reversed.

Example: 12321
Output:
Yes, a Palindrome number

Example: RACECAR
Output: Yes, a Palindrome string

It is evident that letters form mirror images on reversal.

Now that you understood the concept, let’s simply dive into a program to check palindrome in Python.

Palindrome Program using While Loop

This is one of the easiest programs to find Palindrome program using while loop. Let’ dive into an example to check whether a given input is a palindrome or not.

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

Output:

Enter a number:121
The number is palindrome!

Moving ahead in Python palindrome program examples, let’s us see how to check a string whether its a palindrome or not using built-in functions.

Palindrome Program( String) using inbuilt Method

 

string=input(("Enter a string:"))
if(string==string[::-1]):
      print("The string is a palindrome")
else:
      print("Not a palindrome")

Output:

Palindrome in Python - Edureka

Explanation: In the above program, first take input from the user (using input OR raw_input() method) to check for palindrome. Then using slice operation [start:end:step], check whether the string is reversed or not. Here, step value of -1 reverses a string. If yes, it prints a palindrome else, not a palindrome.

This brings us to the end of this article where we have learned how to find palindrome in Python. Hope you are clear with all that has been shared with you in this tutorial.

Make sure you practice as much as possible and revert your experience.  

Got a question for us? Please mention it in the comments section of this “Palindrome program in Python” blog and we will get back to you as soon as possible.

To get in-depth knowledge on Python along with its various applications, you can enroll for live Python Certification Training with 24/7 support and lifetime access.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.