Can we call more than one string method in a single statement?


#1

Question

In the context of this lesson, can we call more than one string method in a single statement?

Answer

Yes, you can call multiple methods, one after the other in the same statement. This is sometimes referred to as “method chaining”. When you chain methods, it applies each method from left to right. The following methods will apply to whatever return value the previous method provided.

For example,

words = 'HeLlO WoRlD'.lower().title().split()

print(words)
# ['Hello', 'World']