Learn Python – Palindrome program in python language- Basic and advance

What is a palindrome?

A palindrome is a variety or letter that remains the same even if the quantity and letters are inverted.

For example:

121, 11, 414, 1221, 74747 are the palindrome numbers.

MOM, DAD, MADAM, REFER are the palindrome letters.

JAVATPOINT, PROGRAM, JAVA are now not the palindrome letters.

Palindrome algorithm

Read the number or letter.

Hold the letter or number in a temporary variable.

Reverse the letter or number.

Compare the temporary variable with reverses letter or number.

If both letters or numbers are the same, print “this string/number is a palindrome.”

Else print, “this string/number is not a palindrome.”

Palindrome Program

Program 1: Palindrome string

str = 'JaVaJ'  
strstr = str.casefold()  
  
# This string is reverse.  
rev = reversed(str)  
  
if list(str) == list(rev):  
   print("PALINDROME !")  
else:  
   print("NOT PALINDROME !")  

Output:

PALINDROME !

Program 2: Palindrome string program

string=input(("Enter a letter:"))  
if(string==string[::-1]):  
      print("The letter is a palindrome")  
else:  
      print("The letter is not a palindrome")  

Output:

Enter a letter: javatpoint
The letter is not a palindrome

Enter a letter: MADAM
The letter is a palindrome

Program 3: Palindrome range program the usage of while loop

Num = int(input("Enter a value:"))  
Temp = num  
Rev = 0  
while(num > 0):  
    dig = num % 10  
    revrev = rev * 10 + dig  
    numnum = num // 10  
if(temp == rev):  
    print("This value is a palindrome number!")  
else:  
    print("This value is not a palindrome number!")  

Output:

Enter the value: 2551
This value is not a palindrome number!

Enter the value: 1221
This value is a palindrome number!