You are viewing a single comment's thread from:

RE: Learn Python Series (#3) - Handling Strings Part 2

in #utopian-io7 years ago

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 ;)

Sort:  

Hi, thx for your lengthy response, and welcome to Steemit by the way!
Besides strip(), I do know about lstrip() and rstrip(). ;-)

I don't see too many use-cases for strip() and its left/right brother and sister. Because when working with lots of (unstructured) textual data, you don't know upfront what can or cannot be stripped without inspecting the data, either manually or with a bunch of algos, and for the latter other methods are far more convenient / precise.

Ref. (as written above):

url = 'https://steemit.com'
domain = url.strip('htps:/')
print(domain)
# eemit.com

When trying to use strip() for parsing URL strings for example, in order to remove the protocol / scheme from the URL, strip() fails when the domain begins with either an 'h', 't', 'p', or 's'.

By the way, have you seen Learn Python Series (#4) - Round-Up #1? In there I've explained how to develop a parse_url() function using only what's been covered in the first three episodes! Without using strip()! :-)

(Also in there, using a for else clause ! :P )
@scipio

Thank you :)

Great and useful exercise. I haven't seem it yet, but I'm going to take time to read your posts soon :) I'm also a enthusiast pythonist, it's always a good idea keep reading and learning new tricks ;)

Honestly I have been inspired by you, I'm going to post tutorial's about great tools, techniques and ideas. I hope soon you could see by here my first tutorials on steemit =)

By the way, I didn't know that the for statement also contains a else clause, great men! Thank you for good content

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 62648.12
ETH 2562.87
USDT 1.00
SBD 2.74