Doubt about Python’s rstrip function

Asked

Viewed 187 times

3

Any idea why the Python command 'i3-7023'. rstrip('-7023') result only in 'i', while the command 'i3-8819'. rstrip('-8819') result in 'i3'? One would not expect the first command to also result in 'i3'?

2 answers

2

I ended up discovering the answer: the rstrip function also removes all character combinations from the argument, right from the string to the first incompatibility. Thus, as the character '3' was present in the argument '-7023', it was also removed. On the other hand, the character '3' was not present in the argument '-8819', so it remained! The hint remains!

2


Translation of Python Documentation 3.8.2 Embedded Types - Python Methods String

str.rstrip([ caracteres ])

Returns a copy of the string with the final characters removed.

The argument caracteres is a string that specifies the character set to be removed. If omitted or None, the argument caracteres by default remove white space.

The argument caracteres is not a suffix, instead all combinations of its values are removed:

>>> '   spacious   '.rstrip()
'   spacious'

>>> 'mississippi'.rstrip('ipz')
'mississ'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.