×

Palindrome In Python

What is Palindrome?

A Palindrome can be defined as the number or a string that resides unchanged when it is reversed.

Example: 14341

Output: Yes, this is a Palindrome number

Example: RACECAR

Output: Yes, this is a Palindrome string

Palindrome Program using While Loop

We can find Plaindrome with the use of while loop. It is very easy to find Palindrome using while loop.

Example:

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("This number is a palindrome!")

else:

    print("This is not a palindrome!")

Output:

Enter a number:454

The number is a palindrome!

Using built-in Functions

Now we are checking the string whether it is a palindrome or not by using the built-in functions.

Example: Palindrome Program using inbuilt Method.

string=input(("Enter a string:"))

if(string==string[::-1]):

      print("This string is a palindrome")

else:

      print("This string is not a palindrome")

Output:

Enter a string: ABCDCBA

This string is a palindrome

Explanation: In the above program, we have checked that the string is palindrome or not. Here, we have taken the input from the user by using the input method and then used the slicing operation for checking whether the string is reversed or not. Then we have printed the result.

Using recursive function

Here, we are using the recursive function for checking if a string is palindrome or not.

Example:

# python code for checking Palindrome

# using recursion

def is_palindrome(s):

    if len(s) < 1:

        return True

    else:

        if s[0] == s[-1]:

            return is_palindrome(s[1:-1])

        else:

            return False

a=str(input("Enter string:"))

if(is_palindrome(a)==True):

    print("This string is a palindrome!")

else:

    print("This string isn't a palindrome!")

Output:

Enter string:malayalam

This string is a palindrome!

Explanation: In the above code, we have used a recursive function for checking a string is palindrome or not.

Using for loop

Here, we are using for loop for checking the numbers whether it is palindrome or not.

Example:

number=input("Enter any number :")

i=0

for i in range(len(number)):

    if number[i]!=number[-1-i]:

        print('This is not a palindrome')

        break

    else:

        print('This is a palindrome')

        break

Output:

Enter any number:7667

It is a palindrome

Explanation: In the above code, we have created a for loop in the program for checking the numbers are palindrome or not.

Conclusion

In the above article, we have discussed different methods for checking the palindrome numbers and a string. Now, you have understood the concept of palindrome in Python.


Related Topics

Find Last Occurrence of Substring using Python

Introduction When planning to work with strings, we may need to determine whether a substring is present. This issue is rather typical, and there have been numerous discussions about how to...

3 minutes read.

Working with CSV files in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

7 minutes read.

OS Module in Python

What is a module? The Modules are the kind of files that contains python statements and definitions. The module is known by the name of the file followed by the suffix...

8 minutes read.

File Explorer using Tkinter in Python

File Explorer: Users can control folders and files on a device using an application known as a file manager or file explorer. Customers can access, edit, copy, delete, and start moving files...

3 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Python if statement

Python if statement Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as: If statementIf-else statementelif statement if statement The if statement is...

2 minutes read.

Imread Python

In this walkthrough, we'll go over the specifics of using the imread() method of OpenCV-Python and various methods for loading images. Imread is an Image reading library. One of the most helpful...

7 minutes read.

Write Dictionary to CSV in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Change Data Type in Python

Python is a dynamic language where it is not always required to consider every variable type. Python supports a wide range of data types, but There are mainly six data...

3 minutes read.

Python SciPy Library

Introduction SciPy, a logical library for Python is an open-source, BSD-authorized library for arithmetic, science, and design. The SciPy library relies upon NumPy, which gives advantageous and quick N-dimensional exhibit control....

6 minutes read.

Python Switch Case

What is a Switch Case Statement? In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once...

3 minutes read.

Data Drop in Python

Introduction You'll understand how to delete a group of rows from a Pandas dataframe in this article.You can read this article on How to Drop Columns in Pandas to find out...

6 minutes read.

Class and Static Methods in Python

The method is an important concept to learn for every programmer or data analyst. We know that in OOP's concept, each object has its attributes and behaviors defined through methods....

3 minutes read.

Check Letter in a String Python

Checking a specific letter or a character in a given string or a string which is taken as an input from the user is possible in Python in broadly three...

3 minutes read.

Augmented reality in python

In this article, we shall learn about augmented reality in python. Augmented reality is a technology that captures the world around you and adds virtual content or objects on top of...

3 minutes read.

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

How to check the version of the Python Interpreter?

As we all know what an interpreter is, and how important it is. We should also be aware of the fact that it is important to have knowledge of the...

2 minutes read.

Python MySQL

In this article, we are going to learn the following: How to connect Python to MySQL.How to create a new Database.Procedure for connecting the newly created database.Procedure for connecting the already...

7 minutes read.

Python Project Ideas Based On Django

Introduction If you have learned Python and you are an expert in Django, then your practical skills should be excellent in this field. If you want to check your practical skills,...

9 minutes read.

How to check if the dictionary is empty in Python?

What is a dictionary in Python? A directory is a collection of data but data is not ordered. Unlike other data types, it does not hold a single value as its...

3 minutes read.