Python String Function Question..

Asked 2 years ago, Updated 2 years ago, 19 views

If you run 'fdsf-fg'.rstrip('-fg') and 'fdsg-fg'.rstrip('-fg') All results are 'fds'. It's normal when fdsf and fdsg come out, respectively. Why does it come out as "fds"?

In "fdsf-fg", instead of "f" in index 3, use the lowercase alphabet except for g If you change it and run it, the alphabet will be printed normally...

python

2022-09-22 20:42

1 Answers

The meaning of arg in String.rstrip is

String.rstrip(SUBSTRING)

Not

String.rstrip(CHARACTORS)

This is.

Among the characters you check by looking from right to left of the string you want to change, It means that all the things that belong to the CHARACTORS that are handed over to arg will be Striped, so it is correct that the results you mentioned above come out.

https://www.tutorialspoint.com/python/string_rstrip.htm

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

You can somehow implement the String method by combining it.

Perhaps we should use regex to implement it in a simple way that is most similar to the intended function.

import re
pattern = re.compile(r'-fg$')
pattern.sub('', 'fdsf-fg-fg')
# # 'fdsf-fg'


2022-09-22 20:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.