25 XP

11. Palindrome check

Beginner strings

A palindrome reads the same forwards and backwards, like level or noon. A phrase can be one too: keep only its letters and digits, put the letters in lower case, and see whether the result reads the same both ways. "A man, a plan, a canal: Panama" becomes "amanaplanacanalpanama", which does.

Write a function is_palindrome(text) that takes a string text and returns True if it is a palindrome in this sense, or False if it isn't:

  • Only letters and digits count. Spaces, punctuation and symbols such as _ are left out.
  • Ignore case: R and r count as the same letter.
  • Digits count just like letters: "0P" becomes "0p", which isn't a palindrome.
  • If nothing is left (an empty string, or only spaces and punctuation), the answer is True: an empty string reads the same both ways.

Example 1

Inputtext = "A man, a plan, a canal: Panama"
OutputTrue
Explanation"amanaplanacanalpanama" is a palindrome.

Example 2

Inputtext = "race a car"
OutputFalse
Explanation"raceacar" backwards is "racaecar".

Example 3

Inputtext = "0P"
OutputFalse
ExplanationDigits count: "0p" backwards is "p0".

Constraints

  • text has 0 to 200 characters.
  • They are English letters, digits, spaces and punctuation.

Submit also runs 12 more tests you can't see.

Practises Strings (lesson 4).

Code
def is_palindrome(text):
    """Check whether text reads the same both ways.

    text: str, letters, digits, spaces and punctuation;
        may be empty
    Returns: bool, True if its letters and digits read the
        same both ways, ignoring case, else False
    """
    # Write your code here
    pass