Lesson 7
Objects and Dictionaries
Chapter 3: Object methods
Palindrome revisited
Now that you have hopefully glanced at the documentation for str
methods,
what better way to practise using these methods by applying them to a practical task?
Remember palindromes from Lesson 6? As a reminder, a palindrome is a word that reads the same forwards and backwards. Example: madam
, level
, rotator
, noon
, kayak
, civic
, malayalam
.
You only implemented a simple version in Lesson 6 (well, at least I hope you did!), where you assume that the input is a single word made up of lowercase letters only.
Your task is to extend the is_palindrome()
function so that it also supports long phrases. Examples:
- Top spot
- Don’t nod.
- I did, did I?
- My gym
- Step on no pets
- Was it a cat I saw?
- No lemon, no melon
- Eva, can I see bees in a cave?
- Sit on a potato pan, Otis
- Able was I, ere I saw Elba.
Your function should return True
for all the above. You can assume that we only consider English letters for determining whether a phrase is a palindrome, and ignore punctuations, spacing and casing.
Sample usage
>>> is_palindrome("kayak")
True
>>> is_palindrome("Step on no pets!")
True
>>> is_palindrome("Eva, can I see bees in a cave?")
True
>>> is_palindrome("Hello, my friend!")
False