A Detailed Explanation of the rstrip Method in Python

Yesterday, while writing a Python program to batch-process Markdown files, I encountered an issue with improper use of the rstrip method. I'm documenting this.

Example of a problem:

1In [1]: a = "dd.md"
2
3In [2]: b = a.rstrip(".md")
4
5In [3]: b
6Out[3]: ''

Looking carefully at the output above, I initially understood that after removing the suffix .md from a, the value of b should be dd, but it is actually empty, which causes the program to malfunction. Because there are many markdown files that need to be processed, and only some of them are abnormal when the suffix is removed, I later consulted the documentation and found that I misunderstood.

The word strip, when used as a verb in English, means:

to remove, pull, or tear the covering or outer layer from something Translated as: remove, peel off, tear off (the outer skin or surface)

When used as a noun:

High-level intellectuals translated it as striptease, which is only one word short of being faithful, accurate and elegant. There are commercial striptease performances in the Western world.

Back to the point, in Python, both the bytes and str types have lstrip , strip , and rstrip methods. Understanding one method is crucial; the others work similarly.

  • lstrip 's l stands for left, processing from the left.
  • strip 's r stands for right, processing from the right.

Official documentation explains:

str.rstrip([chars])

rstrip returns a copy of the original string with the trailing characters removed. The chars parameter specifies the characters to be removed. If omitted or None, chars defaults to removing whitespace (including spaces, newlines, carriage returns, and tabs). The chars parameter doesn't actually specify a single suffix; instead, it removes all possible combinations of the parameter's values. The key is the "combination" keyword. Simply put, any element that matches the chars combination will be removed in that order, repeating until it no longer matches the suffix. Here's an official example:

1>>> 'spacious'.rstrip()
2'spacious'
3>>> 'mississippi'.rstrip('ipz')
4'mississ'

Without the chars parameter, whitespace is removed to the right.

When the 'ipz' parameter is included, all the items in the 'ipz' set at the end are deleted, regardless of the order or quantity. Please see my example:

 1In [4]: 'mississippi'.rstrip('pzi')
 2Out[4]: 'mississ'
 3
 4In [5]: 'mississippi'.rstrip('zip')
 5Out[5]: 'mississ'
 6
 7In [6]: 'mississippi'.rstrip('zip')
 8Out[6]: 'mississ'
 9
10In [7]: 'mississippi'.rstrip('izip')
11Out[7]: 'mississ'
12
13In [8]: 'mississippi'.rstrip('izzizzzppp')
14Out[8]: 'mississ'
15
16In [9]: 'mississipppzzzziii'.rstrip('izzizzzppp')
17Out[9]: 'mississ'

As long as you are in this set(chars), you can kill accurately without any ambiguity, of course, the premise is that you want to kill whichever side.

This scene is like a robot killer holding a stirp machete, and someone has ordered that the people in the chars death note must be killed 🤣😅.

Back to the example at the beginning of the article:

1In [11]: 'dd.md'.rstrip(".md")
2Out[11]: ''

It just so happens that '.', 'd', 'm' are all on the death list, leaving no one behind, and the final value is empty.

The problem is, if you just want to simply remove a single suffix, rather than all the characters in the given set, then use the str.removesuffix() method.

  • Official example:
1>>> 'Monty Python'.rstrip(' Python')
2'M'
3>>> 'Monty Python'.removesuffix(' Python')
4'Monty'
  • My example
1In [13]: 'dd.md'.removesuffix(".md")
2Out[13]: 'dd'
3
4In [14]: 'helloword.txt'.removesuffix(".txt")
5Out[14]: 'helloword'

Correspondingly, the removeprefix method will remove a single prefix string, not all characters in the given set:

  • Example, remove Tel: in front of a phone number
1In [16]: 'Tel:086-13110119120'.removeprefix("Tel:")
2Out[16]: '086-13110119120'

This article uses rstrip as an example. The principles of lstrip, strip, and lstrip are similar, so I won't list them all here. If you're interested, try them yourself.

This shows that true knowledge comes from practice. The devil is in the details; mastery comes from frequent practice and extensive testing.

Lastmod: Friday, August 8, 2025

See Also:

Translations: