Check Palindrome String in Python

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

BeginnerString ProgramsExample 2 of 25
check-palindrome-string.py
Run in browser
1# Program to check if a string is a palindrome
2
3s = input("Enter a string: ")
4
5normalized = "".join(ch.lower() for ch in s if not ch.isspace())
6
7if normalized == normalized[::-1]:
8 print("Palindrome")
9else:
10 print("Not a palindrome")

Output

Enter a string: Never odd or even
Palindrome

What's going on

We normalize by:

Lowercasing all characters
Removing spaces

Then we compare the string to its reverse using slicing.