Palindrome Program in Python using for loop [New]

We will check whether the number or string is a palindrome or not in python, using for loop ,while loop and also using reversed() function.
Table of Contents

Palindrome program in python using for loop

number=input("Enter any number/word :")
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')
Output:

Enter any number:5665
It is a palindrome

Palindrome String 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.

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 accepted 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
  
  

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

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

 
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.  

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.