Home python Strange behavior str.rstrip () method in Python 3.7

Strange behavior str.rstrip () method in Python 3.7

Author

Date

Category

Solving the puzzle of Yandeks.Litseya faced with a curious problem. The fact is that when you try to cut the line [email protected]with the help of built-in method rstrip () turns out not quite correct (from my perspective) result. In the following passage:

print ('[email protected]'.rstrip ( '@ untitled.py'))

the output is a string

nikita_nik

At the time, as the output expected string “nikita_nikitin”.

Also note that the following commands is more correct:

print ('[email protected]'.rstrip ( '@ untitled.py'))
print ('[email protected]'.rstrip ('[email protected] '))

The output is:

nikita_nikitin9
nikita_nik

Checked only in Python 3.7, as an IDE – PyCharm.

In connection with this question:
What is still caused by such behavior and how the interpreter explained the problem?


Answer 1, Authority 100%

The documentation the method str. rstrip ([chars]) says:

Return a copy of the string with trailing characters removed. Their
chars argument is a string specifying the set of characters to be
removed. If omitted or None , the chars argument defaults to removing
whitespace. The chars argument is not a suffix; rather, all
combinations of its values ​​are stripped:

Free translation (in part):

Returns a copy of the string with the remote end characters. Argument
chars – a string that specifies a set of characters to delete

.


Simply put, all the characters will be removed before the first inappropriate.

In your example, the first character from the end, which is not part of the collection @ untitled.py – it k . Everything else will be deleted.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions