strip() method makes me confused
On 2020-11-07 1:03 PM, Bischoop wrote:
> According to documentation strip method removes heading and trailing
Both are explained in the docs -
> Why then:
> txt = ",,,,,rrttggs...,..s,bananas...s.rrr"
> x = txt.strip(",s.grt")
> output: banana
"The chars argument is not a prefix or suffix; rather, all combinations
of its values are stripped"
As you can see, it has removed every leading or trailing ',', 's', '.',
'g', 'r', and 't'. It starts at the beginning and removes characters
until it finds a character that is not in the chars arguments. Then it
starts at the end and, working backwards, removes characters until it
finds a character that is not in the chars arguments.
> another example:
> text = "this is text, there should be not commas, but as you see there
> are still"
> y = txt.strip(",")
> this is text, there should be not commas, but as you see there are still
As you yourself said above, it removes 'leading and trailing
characters'. In your example, the commas are all embedded in the string.
They are not leading or trailing, so they are not removed.
Note that in Python 3.9, 2 new string methods have been added -
If the string starts with the prefix string, return
string[len(prefix):]. Otherwise, return a copy of the original string
If the string ends with the suffix string and that suffix is not empty,
return string[:-len(suffix)]. Otherwise, return a copy of the original