Great post! I didn't know that behavior of the function strip (I just had been using it to erase spaces from the start and the end of the string), but I see it useful. In case of text formatting and validation, if you want to strip the apparition of some phrase at the beginning or the end of the string.
Without that function you could do
"the phrase here, and then the all text".replace ("the phrase here", "")
but it will erase ALL apparitions of the phrase in the text. So you could end doing
"".join("the phrase here, and then the all text".split("the phrase here")[1:])
But, what if you want to strip a phrase at the end of the text? Here the strip function is more useful, because without it we would need to reverse the text and also the phrase to be striped so it can work. Code:
"".join("the all text, the phrase at the end"[::-1].split("the phrase at the end"[::-1]))[::-1]
In the last case it's much better just use
"the phrase here, and then the all text".rstrip("the phrase here")
By the way, there is also lstrip
(left strip) and rstrip
(right strip) functions at you want by example only strip phrases at the beginning or the end of the string respectively, like I did in the last example ;)
RE: Learn Python Series (#3) - Handling Strings Part 2