Remove Whitespace (Solution)
Now, the string method to do this is
.strip(). So you can say, for example,
string3.strip() and that’ll remove the whitespace both at the beginning as well as at the end, which means that you could use
.strip() for each of those strings, and it’ll do what you’re asked to do.
string2 I can use
.rstrip(), which does the opposite. It just removes from the right side but not from the left. And to show you the difference, so if I would use any of those on
string3, so for example,
string3 is going to remove the whitespace on the left, but it’s going to leave it on the right. And the opposite, if I say
string3.rstrip(), then you can see that the whitespace on the left stays, but the one on the right gets removed.
And we’ve also learned that you cannot paste multiple lines in one go into the Python interpreter. That’s going to cause a
SyntaxError where Python just tells you, no, this doesn’t work, but you can paste one line at a time.
Become a Member to join the conversation.