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.
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