Python program to check whether word is palindrome or not!

Python program to check whether word is palindrome or not!

Vipul kunwar's photo
Vipul kunwar
·Feb 27, 2022·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • What is a palindrome?
  • Why use palindrome?
  • How to check palindrome with Python Program?
  • Conclusion:

You played many word games. I’m sure palindrome too.
If you don’t know what a palindrome is? How to check the palindrome with python and why palindrome is useful then this blog is for you.

What is a palindrome?

A palindrome is a word, numbers that are the same from starting and end. For example, madam and radar are the same from starting and end. Hence, you can say that both words are palindrome.

So, these are interesting words. Drop three palindrome words in the comment section.

You can also watch the YouTube video for this program:>

Why use palindrome?

In the previous Section, you’d seen what is a palindrome?
Here, you’re going to see why to use palindrome?
Palindrome helps you with Brain Teaser.

That means it’ll exercise your brain muscles and you’ll get good at a word game.
Other than that, you can check the words or numbers are palindromes or not?

Do you know?

The first known palindrome is in Latin, dating back to Roman times; it reads: "Sator arepo tenet opera rotas". This word is the same from forward and backwards. So, the next question comes how to check palindrome using Python?

Let’s see!

How to check palindrome with Python Program?

In this section, I’m going to write a Python program for palindrome.

The first line of code takes the string as user input. This string we’ve to check whether it is palindrome or not?
Here's how:

# Program to check if the string is palindrome or not

my_str = input("Enter the string you want to check whether it's palindrome or not:")

As you know, the string can be in uppercase. So, we’ll make it in lowercase.
Hence, In the second line of code, I’ve used casefold() method. The second line of code is below:

# Make all in lowercase
my_str = my_str.casefold()

So, as you know we’ve to check the palindrome of the string from backwards and forward. That’s why I’m going to use reversed() method to reverse the string.
Here’s how:

# reverse the string
rev_str = reversed(my_str)

From the above, steps you can see that I’ve taken the string as the user input.
I also reversed the string.

Next, I’m going to check whether an original string is a palindrome of the reversed string.
Here, you can’t compare the string, because the string is immutable and irreversible. So, I’m going to convert the string into a list and then compare the string.
I’ve chosen the list because it is a mutable data type.

Here's how:

# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
    print("The string",my_str,"is a palindrome: ")
else:
    print("The string",my_str,"is not a palindrome!")

We need to check whether the program satisfies the condition or not, Thus I’m going to use an if-else statement.

When a string is a palindrome then if statement executes Otherwise else statement executes.
So, let’s run the program. I’ve run it in thonny IDE.

# Program to check if the string is palindrome or not

my_str = input("Enter the string you want to check whether it's palindrome or not:")

# Make all in lowercase
my_str = my_str.casefold()

# reverse the string
rev_str = reversed(my_str)

# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
    print("The string",my_str,"is a palindrome! ")
else:
    print("The string",my_str,"is not a palindrome!")

Output:

Python 3.7.9 (bundled)
>>> %Run 'Practical 9-- string is palindrome or not.py'
Enter the string you want to check whether it's palindrome or not:madam
The string madam is a palindrome!

After running the program, it’ll take the user input. I’ve given user input madam. You can see that it’s showing the string is a palindrome message.

Thus, you’ve seen the program for palindrome in Python.
Likewise, you can check the numbers are palindrome or not.
You can also use Python for loop to check the palindrome of the string.
Using palindrome you can check strings and numbers.
Do you know? In number, 22-02-2022 is the palindrome number.

Conclusion:

Finally, you’ve seen the Python program for the string is palindrome or not!
First, we’ve taken the user input that is a string.
Second, I’ve use casefold() function. So, that string is in one case only.
Third, as you know palindrome is where we check strings same for forward and backwards.

Hence, I’ll reverse the string using reversed() method. As you know, the string is immutable. So, we can’t compare strings. Hence, I’ve converted the string into a list. Then put it into an if-else statement.

When the program run we put the input and Python shows us whether the string is palindrome or not!

So, what do you think about the palindrome python program? Drop below! You can also follow me on Twitter. So that you can get micro-content of the Python blog post.

Did you find this article valuable?

Support Vipul kunwar by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this