209. Python Program to Check If a Sentence Is a Palindrome Ignoring Spaces
MediumPhase 2 · Practice
This one takes a few steps. It uses string methods like isalpha() and lower(), and the slice [::-1]. You know them all.
Overview
A palindrome reads the same forwards and backwards, like level. Checking a whole sentence is a common interview question, because you must clean the text first.
Problem Statement
Leila collects sentences that read the same backwards, like Never odd or even. Leila wants a program to check new ones.
- Only letters and digits count. Leave out spaces and punctuation (signs like
,.?and:). - Capital and small letters are the same:
Nandnmatch. - Print
The sentence is a palindromeorThe sentence is not a palindrome.
Examples
Example 1. Input Never odd or even gives:
The sentence is a palindrome
Why: without spaces and capital letters, it is neveroddoreven. Backwards, it is exactly the same.
Example 2. Input race a car gives:
The sentence is not a palindrome
Why: the clean text is raceacar. Backwards, it is racaecar, which is different.
Input
One line of text, with at least one letter or digit. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
One line: The sentence is a palindrome or The sentence is not a palindrome. The tests try 8 different inputs, also sentences with commas, question marks and digits.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.