Check Palindrome String

Check whether a given string is a palindrome (ignoring case and spaces).

PythonBeginner
Python
# Program to check if a string is a palindrome

s = input("Enter a string: ")

normalized = "".join(ch.lower() for ch in s if not ch.isspace())

if normalized == normalized[::-1]:
    print("Palindrome")
else:
    print("Not a palindrome")

Output

Enter a string: Never odd or even
Palindrome

We normalize by:

  • Lowercasing all characters
  • Removing spaces Then we compare the string to its reverse using slicing.