Check Palindrome String
Check whether a given string is a palindrome (ignoring case and spaces).
BeginnerTopic: String Programs
Python Check Palindrome String Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# 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
Understanding Check Palindrome String
We normalize by:
•Lowercasing all characters
•Removing spaces
Then we compare the string to its reverse using slicing.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.