Can someone please do this? Shouldn't the print command print There is a space
. Here it is skipping as well while it should not. TIA
Asked
Active
Viewed 48 times
-1

MisterMiyagi
- 44,374
- 10
- 104
- 119

Nishanth Nair
- 17
- 1
- 4
-
3`str.rstrip()` interprets its argument to be a collection of characters you want removed. `'queue'.rstrip('eu')` results in `'q'`. If you're using Python 3.9+ it you can do `some_sentence.removesuffix('at the end')`. – Steven Rumbalski Aug 18 '22 at 19:22
-
3"*Can someone please do this?*" Do *what*, exactly? [ask] – esqew Aug 18 '22 at 19:23
-
_The chars argument is a string specifying the set of characters to be removed._ ([docs](https://docs.python.org/3/library/stdtypes.html#str.rstrip)). So it doesn't remove the string, but all chars that it contains. – bereal Aug 18 '22 at 19:23
-
2This has bitten me before and I've been working with python for a long time now. The [current answer](https://stackoverflow.com/a/73408470/2089675) suggesting `removesuffix` is exactly the solution that worked for me in the past and is the recommended way – smac89 Aug 18 '22 at 19:29
-
@Elliptica I've rolled back your edit since it seems to have shifted the question just onto the next problem. With the selected wording, it seemed to have asked for the *purpose* as to why `rstrip` works as per spec, or at least that would be one way to read it. That doesn't match the existing answer and is likely unanswerable to begin with. – MisterMiyagi Aug 18 '22 at 19:40
-
1[Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied, or even consumed by users of adaptive technologies like screen readers. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Aug 20 '22 at 23:46
1 Answers
1
The argument of the method rstrip(characters)
is a string of characters that will be removed from the right. That means, that the right-most char in the some_sentence
string is in the set characters
, it will be removed. And that's done until the right-most char is not in the set to be removed.
See https://docs.python.org/3/library/stdtypes.html?highlight=rstrip#str.rstrip
What you want to accomplish is done with .removesuffix()

Echedey Luis
- 11
- 3