Python Program to Check a String and Number is Palindrome Or Not

A word, phrase, or sequence that reads identical backwards as forwards. For Example – dad, mom, madam, 121, 1120211

Python Program to Check a String is Palindrome Or Not

To understand this example, you should have knowledge of following Python programming topics:

Source Code – Python program to check if a string is palindrome or not

# Palindrome Example
# Palindrome String - dad, mom, madam, 121, 1120211

myString = "Madam"
myString = myString.casefold()
revString = reversed(myString)

if list(revString) == list(myString) :
    print( "String is Palindrome")
else:
    print( "String is not Palindrome")

It’s also available on GitHub – https://github.com/studygyaan/python-tutorial/blob/master/Python-Palindrome.py

Source Code – Python program to check if a number is palindrome or not

Python program to check if a number is palindrome or not
# Python Program for Palindrome Number 

num = int(input("Enter a number: ")) 

temp = num 
rev = 0 

while num>0: 
    remain = num % 10 
    rev = (rev * 10) + remain 
    num = num // 10 

if temp == rev: 
    print("Number is palindrome") 
else: 
    print("Number is not palindrome") 

It’s also available on GitHub – https://github.com/studygyaan/python-tutorial/blob/master/Python-Palindrome-Number.py

Share