×

Palindrome program in Python

Palindrome program in python

A number or string is said to be a palindrome if we invert the number or string and the string or number remains the same as the original number or string.

Example:

Input: TUTURIALANDEXAMPLE 
Output: This is not a palindrome  
Input: 151 
Output: This is a palindrome

Rule to check palindrome:

  • Read the given input and hold it in the temporary variable.
  • Reverse the input.
  • Compare temporary variable with reversed input.
  • If both are the same, the input string or number is a palindrome.
  • Else input string or number is not a palindrome.
  • End the program.

Example 1:

The following is a basic source code to check the palindrome:

 ## Take the input
 string = "JaHaJ"  
 string1 = string.casefold()   
 #Reverse the string 
 revString = reversed(string) 
 # Check the palindrome 
 if list(string) == list(revString): 
    print("The string is Palindrome.")  
else:   
    print("The string is not Palindrome.") 

Output

Here is the result:

Palindrome program in Python

Explanation: In this program, we have taken a string and store it into a variable string1. The method casefold() makes it suitable for caseless comparisons and returns the string in lowercase. The reversed() function will reverse the original string. Since this function returns a reversed object, we use the list() function to convert them into a list before comparing. Finally, we will compare this string with the original string using the IF statement. If both strings are the same, it will print the string as a palindrome. Else it prints string is not a Palindrome.

Example 2: Palindrome string program using inbuilt function

Here is the source code:

 #Take input from a user   
 string = input(("Enter a string:"))    
 # Check the palindrome    
 if(string==string[::-1]):  
       print("The string is a palindrome.")   
 else:   
       print("The string is not a palindrome.")   

Output

CASE 1:

Palindrome program in Python

CASE 2:

Palindrome program in Python

Explanation: The extended slice operator will reverse the string and then compare it with the original string using the IF statement. If both string matches, the string is a palindrome. Else the string is not a palindrome.

Example 3: Palindrome Program using While Loop

The source code is given below:

 # Take user input
 number = int(input("Enter a number:"))  
 temp = number                                
 reverse=0                                                          
 # Check number is greater than zero  
while(number>0):    
     digit = number % 10
     reverse = reverse * 10 + digit 
     number = number // 10   
 if(temp == reverse): 
     print("The number is palindrome!") 
else:  
     print("The number is not a palindrome!")

Output

CASE 1:

Palindrome program in Python

CASE 2:

Palindrome program in Python

Explanation: In this program, a user will be asked to enter a number to check if the number is palindrome or not. This number will store in a variable named number. A temporary variable temp is used to hold the value of the number during the execution of the program, and another variable, reverse, is also declared and initialized as value 0.

We will obtain each digit of the given number by using the modulus operator %. The reverse value will be calculated using reverse * 10 + digit and get updated. This process will continue till the value of the number become zero.

Now, if(temp == reverse) condition checks whether the input number is exactly equal to the reverse number or not. If this condition is true, then it is a Palindrome. Else the given number is not a Palindrome number.


Related Topics

Python Set update() method

Python Set update() method The set.update() method in Python updates the current set, by adding items from another set. If an element is common in both the sets, only one appearance of this item...

1 minute read.

N2 in Python

N2 is known as Nearest Neighbor Algorithm, because it contains 2 N's (N-Nearest, N-Neighbor). This is a library in the python build using C++ and Python. Before N2 was made,...

3 minutes read.

Operator Module In Python

Introduction The operator module is used for performing operations using methods rather than utilizing operators in Python code. The operator module provides several methods for performing the operations.  The operator module contains...

7 minutes read.

Import Function in Python

In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure...

3 minutes read.

What is Ipython shell?

IPython is a command shell for computing in multiple programming languages. IPython was developed for python language by Fernando Perez in 2001 as a well-equipped python interpreter that offers shell...

3 minutes read.

Python BS4 Code

Python BS4 Code The BS4 stands for BeautifulSoup version 4.x. The BeautifulSoup is a Python library which is used for pulling out data of the HTML & XML files using the...

14 minutes read.

Program of Cumulative Sum in Python

Introduction This tutorial, explains what is meant by lists, the cumulative total, several approaches to calculating the cumulative sum of digits in a list, etc. Learn the straightforward Python codes for...

5 minutes read.

Python Project Ideas for Beginners

Python project ideas for beginners Advanced Python is an amazing platform to grow and achieve success as a developer in the future. Python is a programming language that can be very...

7 minutes read.

API Requests using Python

What is an API? API stands for Application Programming Interface. It is commonly known as API. It provides an environment that helps two or more computer programs to contact each other....

5 minutes read.

Converting Set to List in Python

Converting ‘set’ datatype to ‘list’ datatype is called typecasting. Typecasting in programming is a method to convert one datatype into another datatype. It may happen implicitly by the defined language, called...

3 minutes read.

Rank Based Percentile GUI Calculator using Tkinter in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

3 minutes read.

Simple GUI calculator using PyQt5 in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

4 minutes read.

How to Install Matplotlib in Python?

How to Install Matplotlib in Python The speed at which the enormous amount of data is generating has become a huge aid in understanding what's going on currently in the market....

4 minutes read.

Function annotations() in Python

What is Function Annotations? Function annotations provide a way related to different parts of a function at compile time with arbitrary Python expressions. It doesn’t have life in Python runtime execution....

3 minutes read.

Python Comments

In this tutorial, we will discuss the importance of comments in the Python code and how various types of comments can be inserted in the code. Comments are used to describe...

3 minutes read.

Speech Recognition Module in Python

Speech Module in Python: Converting text to speech, known as Speech Synthesis, this process is the computer-generated recreation of human speech. This module converts the human language text into human-like...

8 minutes read.

Insertion Sort using Python

Insertion sort is a type of sorting technique that is used for sorting an array with random elements. Using sorting methods, any unsorted array can be sorted into ascending or...

3 minutes read.

GUI Calculator in Python

Introduction In Python, we can develop a GUI(Graphical User Interface) with multiple options. It offers us some great and commonly used methods for the development of the Graphical User Interface. Tkinter...

4 minutes read.

Python Multiprocessing Processor

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python Program to Check Leap Year

Python Program to Check Leap Year Leap year: We all know that each year has 365 or 366 days. But whenever a year has 366 days, then the year is said...

2 minutes read.