×

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

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

Python random.seed() function

The random module in Python produces a random number or pseudo-random data, that is, deterministic. The seed function records the state of a random function to provide the same random...

6 minutes read.

Python max() function

Python max() function The max() function in Python returns the largest item in an iterable or the largest of two or more arguments. Syntax max(iterable, *[, key, default])               or max(arg1, arg2, *args[, key])   Parameter arg1, arg2, *args: This...

1 minute read.

Python Argmin

Introduction The argmin function is defined as numpy.argmin(). This function returns the index of the minimum value or element from a Numpy array in a specific axis. An array is taken...

3 minutes read.

How to Define a Function in Python?

What is a Function? In programming, a piece of code that executes a specific task or a group of related operations is known as a function. What does Python Functions Do? If you...

6 minutes read.

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to...

3 minutes read.

Falcon Python

Introduction of Python Python is the fastest and the smartest programming language and it is an object oriented language. Python has libraries that can be importedeasily and perform many operations; to...

3 minutes read.

Poolmanager in Python

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.

Difference between Mutable and Immutable Objects

Difference between Mutable and Immutable Objects As we all know that Python is an object-oriented programming language. Object-oriented programming approach is based on the objects and the classes’ stores the data...

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

Python Set add() Method

Python Set add() Method The set.add() method adds the specified element to a set. If the element is already present in the set, it doesn't add it. Syntax set.add(element) Parameter element- This parameter represents the element that...

1 minute read.

Python String Negative Indexing

This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing. What is an Index? An index is a numeric value, which is assigned...

3 minutes read.

Executing Shell Commands in Python

This tutorial aims to make us understand what a Shell is, what is the importance of a Shell, what are Shell commands in Python, and how can we execute the...

3 minutes read.

Import Module in Python

In this article, you will learn everything about “ Import ” in Python. Modules in python that are already created can be accessed and used in another code by importing the...

6 minutes read.

Find key from value in dictionary python

Python: Python programming language is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a...

5 minutes read.

Introduction to Scratch programming

Scratch programming is generally designed for children who may create digital stories, games, and animations using the computer language Scratch, which has the largest kid-focused community in the world. Scratch...

3 minutes read.

Idle python download for Windows

Here, we will be discussing how to get the answer to all questions related to installing Python on Windows. What is IDLE? Integrated Development and Learning Environment, or IDLE, is a shorthand....

3 minutes read.

Is Python Case Sensitive

Case sensitivity is the mode of dealing with the written alphabet. The cases of the alphabet are examined and based on these words are being treated. The uppercase and lowercase...

3 minutes read.

Sys Module in Python

What are Modules? The Modules are the kind of files that contain Python statements and definitions. The module is known by the name of the file followed by the suffix “.py”....

5 minutes read.

SKLearn Model Selection

The model selections of SK learn has many functions with which we can work on.It has functions to cross-validate the model and, it also provides validation and learning curves.It is...

3 minutes read.