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
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.

After this program

If the idea is still fuzzy, read Day 4: Strings and String Methods in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 string programs, or see every topic on Python exercises.