×

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

How to Write a Configuration file in Python

This article will discuss How to write a configuration file in python, why we need config files in Python, the format of the configuration file, file extensions, and how to...

11 minutes read.

How to Install PIP In Python

How to Install PIP In Python The libraries for Python have made our work easier than we expected. From a simple addition of two numbers to applying algorithms on the big...

4 minutes read.

Selection Sort Using Python

Selection sort is a type of sorting algorithm which is based on sorting elements in increasing order or ascending order through comparison. This sorting technique does not take extra space...

3 minutes read.

Python String isupper() method

Python String isupper() method The String.isupper() method in Python returns a boolean value ‘true’ if all cased characters in the string are uppercase else for any other value is returns false. Syntax String.isupper() Parameter NA Return This...

2 minutes read.

Python Setup guide and installation

Python Installation guide Step by Step Python is widely used high-level programming language. The first version of Python was launched in 1991. Since then, Python has been gaining popularity. It is considered as...

4 minutes read.

YOLO Python

YOLO means you only look once.It is a technique to perform object detection is called YOLO. It is the algorithmused by the program to identify items in the image. Earlier detection...

4 minutes read.

Python Logging Maxbytes

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....

6 minutes read.

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

Python If-else statement

In real life, there are situations where we have to make decisions for a particular circumstance and based on those decisions, and we plan our next move. The same thing...

4 minutes read.

How to Parse JSON in Python

JSON The JSON, the Java Script Object Notation, is an open standard file designed for data interchange; it is light-weighted text. The JSON uses human-readable text to store and transmit the...

4 minutes read.

Python Set symmetric_difference_update() method

Python Set symmetric_difference_update() method The set.symmetric_difference_update() method in Python updates the original set by removing items that are present in both sets and inserting the other items of the set. Syntax set.symmetric_difference_update(set1) Parameter set- This parameter represents the first...

2 minutes read.

Python format() function

Python format() function The format() function in Python formats a specified value into the given format. A ‘TypeErrorexception’ is raised if the method search reaches the object and the format_spec is non-empty, or if either the format_spec or the...

1 minute read.

Artificial intelligence mini projects ideas in python

Artificial intelligence "The human race started from the invention of the wheel", and today we are about to advance to the Industrial level 4.0, where machines can work, talk, think and...

6 minutes read.

Python program to find Fibonacci series

Python program to find Fibonacci series A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence...

2 minutes read.

How to Practice Python Programming

Learning python kkis a step towards coding. Python gets one closer to programming languages. It is essential to practice every programming language to become a professional in coding. It is...

4 minutes read.

Convert string into int in Python

String A string is defined as a series of characters, special characters, and numbers. A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements...

2 minutes read.

What is the re.sub() function in Python

There. sub () function is used to return a string by replacing the occurrences of a specific character or pattern with a replacement string. To use this function, import the...

3 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

How to create a file in Python?

How to create a file in python? Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that...

3 minutes read.

Python reversed() Function

Python reversed() Function The reversed() in Python returns a reverse iterator. Syntax reversed(seq) Parameter seq: This parameter represents any iterable object. Return This function returns a reversed iterator object. Example 1 # Python Program describing # the reversed() function ...

1 minute read.