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

What is palindrome ?

A palindrome is a word, phrase, number, or another sequence of characters that reads the same forwards and backward. For example, “racecar” and “level” are both palindromes because they read the same forwards and backward.   

Palindrome String Program in Python using for loop

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

Output:

Enter any number:5665
It is a palindrome

Palindrome Program in Python using while loop

In this program, we will take two pointers i and j. Variable “i” will point to the leftmost characters of the string and “j” will point to the rightmost character. If they are the same then will increase “i” by 1 and decrease j by 1 and so on. If at any index, the characters present at these indexes are different then the given string is not a palindrome.

str=input("Enter String: ")
i=0
j=len(str)-1
isPalindrome=True
while i<=j:
    if(str[i]!=str[j]):
        isPalindrome=False
        break
    i+=1
    j-=1

if isPalindrome:
    print(f"{str} is Palindrome.")
else:
    print(f"{str} is not a Palindrome.")
    

Output:

Enter String: racecar
racecar is Palindrome.

You may want to read this post :

Python program to check prime number

Video Solution

Using an inbuilt reversed function

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 to check whether the given string is palindrome or not using an inbuilt reversed function:

  • 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”.

Valid Palindrome leetcode solution in python

First, we have to remove all non-alphanumeric characters in the given string, and then convert all upper-case letters into lower-case letters. Then we have to check whether the converted string is a valid palindrome or not. A valid palindrome is one which read the same forward and backwards.
Problem Link

import re
class Solution:
    def isPalindrome(self, s: str) -> bool:
        str1="".join(re.split("[^a-zA-Z0-9]*",s)).lower()
        i=0
        j=len(str1)-1
        isPalindrome=True
        while i<=j:
            if str1[i]!=str1[j]:
                isPalindrome=False
                break
            i+=1
            j-=1
        return isPalindrome
  
  

FAQ

What is a palindrome number in python using for loop?

A number is a palindrome when its reverse is also the same as the original number.
To check whether a number is a palindrome or not, we have to reverse the number using the reversed() function. Then we compare the actual and reversed numbers using the if statement.
for example: 8558, is a palindrome number. 987 is not a palindrome number.

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, and then we print the number entered as a palindrome. Else, we print the number entered is not a palindrome.

Programs you may like:
Menu-driven program in python using while loop
Factorial Program in Python using for loop

About this Post:

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.  

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.