How to check word is a palindrome or not using Python for loop?

How to check word is a palindrome or not using Python for loop?

Vipul kunwar's photo
Vipul kunwar
·Mar 8, 2022·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Breakdown the Palindrome problem in Python
  • Take user input in Python
  • Use Python for loop to reverse the Python word
  • Run the Python palindrome program
  • Conclusion:

When you were a kid you played with words. Making alphabets, reversing the words, all kinds of stuff you were doing. And I’m sure you got pretty excited about the words that are the same from starting and end. So, I'm going to excite you again.

You can see video of the Python palindrome

Before starting this let’s understand what is a palindrome? A palindrome is a word or numbers that are the same from starting and end.
Here it means the word that is the same from starting and end.

Ok, this time you’re not only going the check the palindrome word but also going to create the Python Program for it.
But wait a minute I’ve done this in my Palindrome word using while loop Program. What’s new in this blog?
In this blog, I’m going to check the word is a palindrome or not using Python for loop.

Yes, you’ve heard right Python for loop.
So, Let’s Create the Python palindrome program and become the kid again!

Breakdown the Palindrome problem in Python

First of all, open your notebook. Let’s think about the problem again! We’ve to create the palindrome python program using for loop. So, here’s a quick breakdown of the Problem.

• Need a word that we want to check for Palindrome
• Reverse the string
• Check whether a string is a palindrome.

Take user input in Python

First, you need a word. You can take a word using the Python input() method. The input() method will take the word as user input. Here’s how it works:

word = input("Enter your word:")

I hope you’ve completed the first step.

Next, we’ve to reverse the Python string. We’ll define the empty rev_str variable. This variable is empty because till here we haven’t reversed the Python string.

rev_str = ""

After it, we need the length of the string because we need to reverse the string using for loop.
Here’s how to get the length of the Python string:-

a = len(word)

Use Python for loop to reverse the Python word

Next, I’m going to use the Python for loop.
A Python for loop will reverse the string.
Here’s how it works:

First, it’ll take the word in for loop then store every word from the last of the word using the word[a-1].
Second, I’m going to use the a-=1 because after each reversing letter it’ll decrease one index.
Third, I’ve used the word[a-1] because in Python indexing starts from 0. That means the 5th character will show using the 4th index.

Here’s how the code looks for the for loop:-

for i in word:
    rev_str+=word[a-1]
    a-=1

So, we’ve taken the word and reversed it.

Now, I’m going to compare the string and reverse string. If both strings are the same then it’s a palindrome string. Otherwise, it’s not a palindrome string.

You can do this with the if-else statement. If-else statement checks the condition.
Here’s what it looks like:

if word == rev_str:
    print(word,"is palindrome!")
else:
    print(word,"isn't a palindrome!")

Run the Python palindrome program

At last, we’ve seen every step of the program. Let’s combine it!

# Check the string is a palindrome or not using Python for loop
word = input("Enter your word:")
rev_str = ""
a = len(word)

for i in word:
    rev_str+=word[a-1]
    a-=1

if word == rev_str:
    print(word,"is palindrome!")
else:
    print(word,"isn't a palindrome!")

When we’ll run this program it’ll show us the below output:

>> %Run 'reverse_string using for loop..py'
Enter your word:madam
madam is palindrome!

So, you’ve seen how to check whether the word is a palindrome or not using Python!

But why did I’ve shown you the Palindrome program using Python?

Using the Python Palindrome program you can solve the different string programs.

This string program will help you make your Python strong. So, not only do you learn the concept but also able to apply it. Using Palindrome you’ve gained insight into loops, strings & reversing strings. This will help you in iterating the sequences in forwarding and backward directions.

Conclusion:

Finally, you’ve seen how to check whether the word is a palindrome or not using Python for loop.

First of all, I’ve used the input() method to take the user_input. Second, I’ve used the for loop to reverse the string. I’ve done reversing the string using indexing. Third, I’ve compared the string with a reversed string using the if-else statement. The most important thing is that we’ve solved the problem using Python for-loop.

So, what do you think about palindrome checker?
Do you’ve another way to check the palindrome using Python then drop below!

To get more Python blogs subscribe to my newsletter.
I’m promising you. You’ll never regret it! No spammy email!
Unsubscribe any time you want. Put your email in starting of this page.
You’ll see an email box. After subscribing you’ll get the welcome message.

Also, if you’re a micro python lover, you can follow me on Twitter.

What you’ll get from Twitter?
You’ll get the special curated Python thread. In the python thread, I’m breaking every Python topic.

If you’ve any questions about my blog, Drop below!
I’ll respond as soon as possible.

Did you find this article valuable?

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

Learn more about Hashnode Sponsors
 
Share this