Palindrome of a number in python

This is a Python Program to check whether a given number is a palindrome.

Show

Problem Description

The program takes a number and checks whether it is a palindrome or not.

Problem Solution

1. Take the value of the integer and store in a variable.
2. Transfer the value of the integer into another temporary variable.
3. Using a while loop, get each digit of the number and store the reversed number in another variable.
4. Check if the reverse of the number is equal to the one in the temporary variable.
5. Print the final result.
6. Exit.

Program/Source Code

Here is source code of the Python Program to check whether a given number is a palindrome. The program output is also shown below.

 
n=int(input("Enter 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 a palindrome!")
else:
    print("The number isn't a palindrome!")

Program Explanation

1. User must first enter the value of the integer and store it in a variable.
2. The value of the integer is then stored in another temporary variable.
3. The while loop is used and the last digit of the number is obtained by using the modulus operator.
4. The last digit is then stored at the one’s place, second last at the ten’s place and so on.
5. The last digit is then removed by truly dividing the number with 10.
6. This loop terminates when the value of the number is 0.
7. The reverse of the number is then compared with the integer value stored in the temporary variable.
8. If both are equal, the number is a palindrome.
9. If both aren’t equal, the number isn’t a palindrome.
10. The final result is then printed.

Runtime Test Cases

 
Case 1
Enter number:121
The number is a palindrome!
 
Case 2
Enter number:567
The number isn't a palindrome!

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Next Steps:

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Palindrome of a number in python

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

Last updated on Aug 11,2022 248.4K Views


Palindrome of a number in python

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

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.

  • What is a Palindrome?
  • Palindrome Program using while loop
  • Palindrome Program using in-built function

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.

Find out our Python Training in Top Cities/Countries

India USA Other Cities/Countries
Data Science with Python Course in Bangalore Data Science with Python Course in New York Data Science with Python Course in the UK
Python Data Science Training in Hyderabad Data Science with Python Course in Charlotte Data Science with Python Course in Dubai
Data Science with Python Course in Delhi Data Science with Python Course in Austin Data Science with Python Course in Canada
Data Science with Python Course in Chennai Data Science with Python Course in Seattle Data Science with Python Course in Singapore
Data Science with Python Course in Kolkata Data Science with Python Course in Dallas Data Science with Python Course in San Jose

Palindrome Program using While Loop

This is one of the easiest programs to find Palindrome program using while loop in Python Programming. 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 of a number in python

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. I 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, or you can join our Data Science with Python Course.

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

Discover your full abilities in becoming an AI and ML professional through our Artificial Intelligence Course. Learn about various AI-related technologies like Machine Learning, Deep Learning, Computer Vision, Natural Language Processing, Speech Recognition, and Reinforcement learning.

Upcoming Batches For Data Science with Python Certification Course

Course NameDate
Data Science with Python Certification Course

Class Starts on 24th September,2022

24th September

SAT&SUN (Weekend Batch)
View Details
Data Science with Python Certification Course

Class Starts on 17th October,2022

17th October

MON-FRI (Weekday Batch)
View Details
Data Science with Python Certification Course

Class Starts on 29th October,2022

29th October

SAT&SUN (Weekend Batch)
View Details

Palindrome of a number in python

Web Scraping And Analytics With Python

Watch Now

Palindrome of a number in python

Introduction to Business Analytics with R

Watch Now

Palindrome of a number in python

Know The Science Behind Product Recommendation With R Programming

Watch Now

Palindrome of a number in python

Application of Clustering in Data Science Using Real-Time Examples

Watch Now

Palindrome of a number in python

Python Programming – Learn Python Programming From Scratch

Watch Now

Palindrome of a number in python

The Whys and Hows of Predictive Modeling-II

Watch Now

Palindrome of a number in python

3 Scenarios Where Predictive Analytics is a Must

Watch Now

Palindrome of a number in python

Python List, Tuple, String, Set And Dictonary – Python Sequences

Watch Now

Palindrome of a number in python

Business Analytics Decision Tree in R

Watch Now

Palindrome of a number in python

Python Classes – Python Programming Tutorial

Watch Now

Palindrome of a number in python

The Whys and Hows of Predictive Modelling-I

Watch Now

Palindrome of a number in python

Sentiment Analysis In Retail Domain

Watch Now

Palindrome of a number in python

Android Development : Using Android 5.0 Lollipop

Watch Now

Palindrome of a number in python

Python Loops – While, For and Nested Loops in Python Programming

Watch Now

Palindrome of a number in python

Mastering Python : An Excellent tool for Web Scraping and Data Analysis

Watch Now

Palindrome of a number in python

What is Mutithreading in Python and How to Achieve it?

Read Article

Palindrome of a number in python

Top 50 R Interview Questions You Must Prepare in 2022

Read Article

Palindrome of a number in python

Implementing K-means Clustering to Classify Bank Customer Using R

Read Article

Palindrome of a number in python

Big Data Analytics: BigQuery, Impala, and Drill

Read Article

Palindrome of a number in python

Python Classes And Objects – Object Oriented Programming

Read Article

Palindrome of a number in python

Data Analyst vs Data Engineer vs Data Scientist: Skills, Responsibilities, Salary

Read Article

Palindrome of a number in python

What are Generators in Python and How to use them?

Read Article

Palindrome of a number in python

Top Machine Learning Interview Questions You Must Prepare In 2022

Read Article

Palindrome of a number in python

Top Deep Learning Interview Questions You Must Know in 2022

Read Article

Palindrome of a number in python

All You Need To Know About R Analytics Course Upgrade

Read Article

How do you find the palindrome of a number in Python?

n=int(input("Enter 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 a palindrome!") else: print("The number isn't a palindrome!")

How do you make a palindrome in Python?

Palindrome Program.
str = 'JaVaJ'.
strstr = str.casefold().
# This string is reverse..
rev = reversed(str).
if list(str) == list(rev):.
print("PALINDROME !").
print("NOT PALINDROME !").

What is the logic of palindrome in Python?

Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome.

Is there any palindrome function in Python?

Using Built-in Functions We can also check palindrome in Python with the help of the built-in reversed() function provided for sequence data types in Python: reversed(sequence) : It returns an iterator that accesses the sequence passed as an argument in the reverse order.