Palindrome in Python
A palindrome reads the same forward and backward after differences such as capitalization and spaces are removed.
What is Palindrome?
A palindrome reads the same forward and backward after differences such as capitalization and spaces are removed.
Check whether text is a palindrome using Python string operations.
When should you use it?
- Learn Python syntax through a practical example.
- Build a foundation for larger programming problems.
- Test an idea quickly without a local setup.
Example code
main.py
text = "Never odd or even"
normalized = "".join(character.lower() for character in text if character.isalnum())
print(normalized == normalized[::-1])
Expected output
True
How it works
The normalized string keeps only letters and numbers. Slicing with a step of -1 produces the reversed value for comparison.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.