Find Palindrome Strings
00:00
Until now, you’ve only used filter()
with lists of numbers. Let’s try out strings and see how you can find palindrome strings using filter()
.
00:11 A palindrome word reads the same backward as forward. For example, “MADAM” and “RACECAR.”
00:20
Your goal is to use filter()
to find the palindrome strings in a tuple of strings and filter out the rest.
00:30
Let’s think about what you need to check in order to see if a word is a palindrome or not. You’re going to have to see if the reverse version of the string is the same as its normal version. Luckily, Python has a built-in function for this: reversed()
.
00:45
Let’s try it out. reversed("Python")
as an example.
00:54
Turns out, reversed()
returns on iterator. To have your result as a string, you’ll have to use the join()
function to turn this iterator into a string.
01:05
join(reversed("Python"))
as the input.
01:12
Here you go. You got Python reversed. Another thing you’ll have to pay attention to is that Python strings are case-sensitive. So "Ana"
with the capital A is not the same as "ana"
with a lowercase a.
01:27
Make sure to use the .lower()
method to make every character lowercase so it’s easier for you to find the palindrome words. Let’s use it once as an example.
01:43
As you can see, you get 'ana'
with every character lowercased. Okay, you’re ready. Time to find palindrome strings. Let’s create a predicate function named is_palindrome
to get word
as an input.
01:57
def is_palindrome()
and word
as the input.
02:05
It should return True
if a string is a palindrome and False
if not. To do that, let’s reverse the input word using the reversed()
function and store it in a variable named reversed_word
. reversed_word =
"".join(reversed())
and word
as the input. Okay, now let’s write up what your function should return.
02:35
Whether the input word.lower()
is equal to the reversed version of it. So reversed_word
also .lower()
. Let’s see if it works.
02:53
Let’s put in "Ana"
with a capital A. is_palindrome("Ana")
. We expect it to return True
, and it does. Now let’s try "hello"
. So is_palindrome()
"hello"
as an input, all lowercase.
03:14
We expect it to be False
, and it is False
. It works, perfect! So for a palindrome string such as "Ana"
, it returned True
, and for a non-palindrome word—for example, "hello"
—it returned False
. So it works great. Now let’s use filter()
.
03:34
list(filter())
. is_palindrome
as the function argument and words
as the iterable. It returned ['Ana', 'madam', 'racecar']
as a result, which is perfect. These are the only palindrome words inside of the tuple words
.
03:55
Here, when you called filter()
, it applied is_palindrome
to every single word in the words
tuple and kept the ones that evaluated as True
and got rid of the rest.
04:07
What you just did in the last few lessons with filter()
and reversed()
is something that is commonly referred to as functional programming.
04:16 Let’s have a look at this programming paradigm in the next lesson.
Become a Member to join the conversation.