Giveaway of Udemy Web Development Course Click Here!

Palindrome Program in Python using for loop [New]

In this program, we are going to check whether the number or string is a palindrome or not, using for loop and also using reversed() function.

In this program, we will check whether the number or string is a palindrome or not in python, using for loop and also using reversed() function.


Table of Contents

Palindrome program in python for numbers using for loop

number=input("Enter any number :")
i=0
for i in range(len(number)):
    if number[i]!=number[-1-i]:
        print('It is not a palindrome')
        break
    else:
        print('It is a palindrome')
        break
Output:

Enter any number:5665
It is a palindrome

Palindrome program in python using string

word=input("Enter any word :")
rev=reversed(word)
if list(word)==list(rev):
    print('It is a palindrome')
else:print('It is not a palindrome')
Output:

Enter any word: madam
It is a palindrome

Approach for Palindrome program in python using string:

  • First, we input the string using the input() function.
  • Using the reversed() function, we reverse the word entered by the user and assign it to a new variable called rev.
  • Using the if statement, we compare the list(word) & list(rev). If they are the same, then we will print "it is a palindrome", and if they are not the same, we will print "it is not a palindrome".

Palindrome program in python for numbers using reversed() function

number=input("Enter any number :")
rev=reversed(number)
if list(number)==list(rev):
    print('It is a palindrome')
else:print('It is not a palindrome')
Output:

Enter any number:7887
It is a palindrome

Approach for Palindrome program in python for numbers using reversed() function:-

  • First, we input the string or number using the input() function.
  • Using the reversed() function, we reverse the number or word entered by the user and assign it to a new variable called rev.
  • Using the if statement, we compare the list(number or word) & list(rev). If these strings are the same, then we will print it as a palindrome; if they are not the same, then we will print it as not a palindrome.

Q. What is a palindrome number in python?

A number is a palindrome when its reverse is also the same as the original number.

To check whether a number is palindrome or not, we have to reverse the number using the reversed() function. Then we compare the true and reversed numbers using the if statement.

for example:8558, is a palindrome

Q. How do you write a palindrome program in python using for loop?

First, we input a number using the input() function, then reverse it using the reversed() function. Then we compare the number (or a word) with its reverse if the actual number and the reversed number are the same, then we print the number entered as a palindrome. Else, we print the number entered is not a palindrome.

 
About this Palindrome Program in Python using for loop

It is my first python program, so please share and comment below to support us. You can also learn python and many other languages from the progate app. You can download it via the google play store.  

You can comment below or contact us if you have any queries related to this program. Thanks for reading this Palindrome program in python.

I hope you enjoy my programs as much as I enjoy offering them to you. If you have any questions or queries related to any program, please don't hesitate to contact me.

1 comment

  1. How can i make palindrome program in python using while loop
Please do not enter any spam link in the comment box.