What is Rstrip() in Python?
The rstrip() in Python is a method used to remove trailing spaces characters from the given argument.
This method can omit a set of characters on the right side of the string specified within its function.
Syntax of String rstrip()
The syntax of the Python string rstrip() method is very simple.
It resembles the syntax below:
string.rstrip([chars])rstrip() parameters
As seen in the above syntax, the rstrip method parameters take the chars value.
This parameter is optional since the program can still run even if there are no chars taken.
Remember that this parameter takes any chars (default Python code for character) that are on the right portion of the string.
Return value from rstrip()
Python rstrip() method returns a copy of the string with trailing characters removed from the declared argument.
The program omits all the string characters that match with what’s within the chars argument.
Until the program meets its function, it will return the remaining characters of the string.
What does rstrip do in Python?
To give more emphasis on how the rstrip method works, let us have these examples.
Example 1: Whitespaces
# testing rstrip find method for whitespaces
txt = "Good day! "
sample = txt.rstrip()
print(sample,"Welcome to this Python Tutorial.")Output:
Good day! Welcome to this Python Tutorial.
Example 2: chars argument
# testing rstrip method with chars argument
message = "Hi there!"
print(message.rstrip("!er"))Output:
Hi th
The precision of the rstrip() function is clear as crystal, as seen in the examples.
Thus, even beginners can perform this method and apply it to their programs.
You can also perform other Python string handling such as Python uppercase and lowercase methods.
What is the difference between strip and Rstrip in Python?
The difference between the strip() and rstrip() method is that the rstrip() can only remove whitespaces on the right side of the string.
On the other hand, the strip() method can remove whitespaces on both the left and right sides.
This applies as long as there was no character value present within the chars argument.
However, both these methods can remove characters from the right side of the string.
The characters that the methods can remove are based on the values declared on chars.
Python rstrip newline
As mentioned in the above discussion, the Python rstrip method is applicable in removing whitespace characters on the right side of the string by default.
However, programmers can also use the rstrip function if there’s a need to remove trailing newline characters.
The rstrip function does not affect the characters in the newline.
Though the strip() method can also perform this function, you can also apply rstrip().
The application of methods depends on the programmers’ choice.
Also, it will vary depending on the program or output that they desire.
Python rstrip multiple characters
Another interesting thing about the Python rstrip is that it can strip multiple characters.
As shown in the previous examples, the python string method (rstrip) can omit multiple characters.
The number of omitted characters will be based on how many would match the values that are in the char argument.
But note that the function can only omit the characters on the right side of the string.
When You Actually Need rstrip() in Real Projects
1. Reading Lines From a File
The #1 reason developers reach for rstrip(): removing the trailing newline character when iterating through a file’s lines:
with open('users.txt') as f:
for line in f:
username = line.rstrip() # strips '\n', '\r\n', spaces, tabs
print(f"Hello, {username}!")Without rstrip(), every printed username would be followed by an extra blank line. This is the single most common rstrip use case in Python projects.
2. Cleaning User Input From input()
name = input("Enter your name: ").rstrip()
# Removes trailing spaces if the user accidentally hits space before Enter3. Stripping a Specific Suffix Pattern
filename = "report_2026_v3.txt"
clean = filename.rstrip(".txt") # WARNING: removes any combination of '.', 't', 'x'
print(clean) # "report_2026_v3" (lucky)
# Safer (Python 3.9+):
clean = filename.removesuffix(".txt")
print(clean) # "report_2026_v3" (guaranteed)Critical gotcha:rstrip(".txt") does NOT strip the literal substring “.txt”. It strips ANY characters in the set {‘.’, ‘t’, ‘x’} from the right side. For “report.txtxt” it would strip all four characters, giving “report”. Use str.removesuffix() (Python 3.9+) when you need to strip a specific string.
Common rstrip() Mistakes and How to Fix Them
- Treating rstrip() argument as a literal suffixit’s a SET of characters, not a substring.
"abc.txt".rstrip(".txt")returns"abc"(all of ‘.’, ‘t’, ‘x’ stripped), not"abc.". Useremovesuffix()for literal-suffix stripping. - Forgetting that rstrip() returns a NEW stringPython strings are immutable.
s.rstrip()doesn’t modifys; you must reassign:s = s.rstrip(). - Using rstrip() instead of strip() when both sides need cleaning
rstrip()only handles trailing whitespace. For both leading AND trailing, usestrip(). - Stripping more characters than intended
rstrip()with no argument strips ALL whitespace types (spaces, tabs, newlines, carriage returns). If you only want to strip newlines:s.rstrip('\n'). - Chaining rstrip() unnecessarily
s.rstrip().rstrip()is redundant; one call strips everything in one pass. The second call is a no-op.
rstrip() vs Alternatives: When to Use Which
| Method | Strips | Best For |
|---|---|---|
rstrip() | Trailing whitespace (or chars in set) | Cleaning file lines, input() |
strip() | Both leading AND trailing | User input with stray spaces on either side |
lstrip() | Leading whitespace only | Removing indentation, leading slashes |
removesuffix() | A specific literal suffix string | File extensions, URL paths (Python 3.9+) |
removeprefix() | A specific literal prefix string | Stripping “https://”, “Mr. ” etc. |
Summary
In summary, this tutorial has covered all of the information needed to understand how the rstrip method works.
Along with the Python rstrip() method, this tutorial also added the string methods that could support the concept of strip.
Either of the mentioned methods, you can try on your programs now.
Aside from this discussion, you can also check read: Easy Ways for Python List Concatenate with Example Codes
Frequently Asked Questions
What does rstrip() do in Python?
The rstrip() method returns a new string with trailing whitespace (or the specified characters) removed from the right side. With no argument, it strips all whitespace types: spaces, tabs, newlines, carriage returns. It does NOT modify the original string, Python strings are immutable.
What’s the difference between strip() and rstrip() in Python?
strip() removes whitespace (or specified characters) from BOTH ends of the string. rstrip() removes them only from the right (trailing) end. lstrip() removes them only from the left (leading) end. Use rstrip when you specifically want to keep leading whitespace intact, for example, when cleaning indented YAML or Markdown.
Does rstrip(“.txt”) strip the literal “.txt” suffix from a filename?
No, this is the most common rstrip mistake. rstrip(".txt") strips ANY characters in the SET {‘.’, ‘t’, ‘x’} from the right side, not the literal string “.txt”. For “abc.txt” it returns “abc”; for “report.txtxt” it returns “report”. To strip a literal suffix, use str.removesuffix(".txt") in Python 3.9+.
How do I strip only a newline character with rstrip in Python?
Pass ‘\n’ as the argument: line.rstrip('\n'). This strips trailing newlines only, leaving trailing spaces/tabs intact. For both ‘\n’ and ‘\r’ (Windows line endings): line.rstrip('\r\n'), the argument is a set, so the order doesn’t matter.
Is rstrip() faster than re.sub() for stripping whitespace?
Yes, by a wide margin, rstrip() is implemented in C and runs roughly 5-10× faster than the equivalent regex re.sub(r'\s+$', '', s). Always prefer rstrip() for simple whitespace stripping. Use regex only when you need pattern matching beyond a character set.
