strip() method makes me confused

Alexander Neilson alexander at neilson.net.nz
Sat Nov 7 06:21:31 EST 2020


Because the strip methods argument is the set of characters to remove from either end. So it checks from the ends character by character until it finds a character that isn’t in the set. Then it removes everything prior to that (or after that at end of the string) and then returns the result. 

So your first example leaves “banana” as the string because all characters after and before that string in the original string were found in the set. 

However in your second example the set only contains the comma and the string neither begins nor ends with commas so if (first character) in (set containing comma) returns false so it stops searching from that end and does the same at the other end. 

https://www.programiz.com/python-programming/methods/string/strip

Hope that helps. 

Regards
Alexander

Alexander Neilson
Neilson Productions Limited
021 329 681
alexander at neilson.net.nz

> On 8/11/2020, at 00:06, Bischoop <Bischoop at vimart.net> wrote:
> 
> 
> According to documentation strip method removes heading and trailing
> characters.
> 
> Why then:
> 
> txt = ",,,,,rrttggs...,..s,bananas...s.rrr"
> 
> x = txt.strip(",s.grt")
> 
> print(x)
> 
> output: banana
> 
> another example:
> 
> text = "this is text, there should be not commas, but as you see there
> are still"
> y = txt.strip(",")
> print(text)
> 
> output: 
> this is text, there should be not commas, but as you see there are still
> 
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list