by BehindJava

Write a program to check palindrome of a string in Python

Home » python » Write a program to check palindrome of a string in Python

In this tutorial we are going to learn about checking a palindrome of a String in Python.

Program to check palindrome of a string

a=input("enter sequence")
b=a[:: -1 ]]
if a==b:
print("palindrome")
else:
print("not palindrome")

Explanation

  1. user must enter a string and store it in a variable.
  2. The string is reversed using string slicing, it is compared to the non-reverse string.
  3. If both are equal, the strings are palindromes.
  4. If they aren’t equal, the strings aren’t palindromes.