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

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']
9 Likes

Why does this result in None as an output?

line_one = "The sky has given over"
line_one_words=line_one.split().append("fyi")
print(line_one_words)

What operation did you get None from, and what does that operation promise to do?

I expect .split() to form a list and the .append() a string into the same list. If I do this in two separate lines it works, but it doesn’t seem to work in one line.

If you do that in two separate lines you get the same result.
You changed something more, you changed the action.

The operations you use make promises to you about what input they are willing to deal with and what output and effects they have. If you leverage those promises, you’ll get what you want, but if you do something for which no promise is made then… no.

So look at the operations you use. What do they promise to do? Where can you find the result after carrying out that operation?

1 Like

Appending to a list adds an element to the end of that list.
Where would you therefore look to get the result? You’d look at the list.

3 Likes

I understood my mistake. Methods such as .append() make changes to the list but returns NONE. Assigning it to a variable make that variable as NONE. I read about this in the forum.

16 Likes

So some methods amend the object as well as return the amended object, while some methods just do the former? And which happens depends on the method used?

This is a late reply but I’m just seeing your question. I don’t think your wording is quite correct. Some methods, such as the .append() method, modify the object itself. When this happens the method returns None not the modified object. Other methods, such as .split(), don’t modify the object they are operating on but instead return some other type of object. In this case a list of strings. Which one happens indeed depends on the method used. As ionatan mentioned all methods and functions set up a contract for the user to follow. As long as the user abides by the contract they will achieve the results they are looking for.

4 Likes

I was intrigued in this problem going through the python 3 course - if anyone is reading this and is curious, the way to solve this on a single line is to use .join and .split like so:

#prev way line_one = "The sky has given over" line_one_words=line_one.split().append("fyi") print(line_one_words) #suggested change line_one = "The sky has given over" line_one_words=line_one.split() line_one_words.append("fyi") print(line_one_words) #new way line_one = "The sky has given over" line_one_words = ' '.join([line_one, 'fyi']).split() print(line_one_words)

My ponderings probably don’t add to the conversation, but maybe someone is tumbling in the same direction.

I was using the type function to follow the type changes;
After the .split().append() combination, the class that line_one_words gets is <class ‘NoneType’>, that’s weird because splitting the action in two lines converts the variable first to a list and then the append can be performed without issue.

My question then was - what is a NoneType class? It’s a fun read:

1 Like

Curious question: does this have anything to do with mixing string methods and list methods (.split vs. .append)?