Python String rstrip() Method

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

The rstrip() function in Python is used to remove the trailing characters at the end of a string. The rstrip() function in Python returns a copy of the provided string with removed trailing characters.
For example, consider a string "Pythonssssss" and we want to remove the trailing 's' from the string. We can use the rstrip() in Python, and "Python" will be returned by the rtrip() in Python.

Syntax of Python String rstrip()

Parameter of rstrip() in Python

chars

It is an optional string that specifies the trailing characters to be removed. If chars are not passed to the rstrip() function in Python, then the trailing white spaces ' ' are removed from the string.

Return Values of rstrip() function in Python

The rstrip() function in Python returns a copy of the given string with trailing characters removed.
All combinations of characters in the chars parameter are removed from the end of the string.

Example of rstrip() in Python

Let's say we have a string "Pythonsssssss" and we have to remove the trailing 's' from the string,

Output:

More Examples of rstrip() Function in Python

Use of rstrip() in Python With Optional Parameters

Given a string s, let's say, "This is a stringssssssss" and we have to remove the trailing 's' from the string. Thus the output should be "This is a string".

Output:

Remove any White Spaces at the End of The String

Given a string s, let's say "Hello World " and we have to remove the trailing white spaces or ' ' from the string. Thus the output should be "Hello World".

Output:

In the above code, you can notice that we still need to provide the optional parameter to the rstrip() function in Python. So by default, trailing white spaces are removed from the string if no parameter is provided to rstrip().

Remove Trailing Characters if they are Commas, s, q, or w

Given a string s, let's say "Scaler,,sq,w,ss,qqqsq,w," and we have to remove the trailing ',', 's','q', and 'w' from the string. Thus the output should be "Scaler".
When we have remove multiple trailing characters from the string we can pass a string as the parameter to the rstrip() function.

Output:

Limitation of rstrip() in Python

Consider a case where the string is "Articlegeeeez" and we want "Article" as output.
You must think that we will pass "gez" as the parameter to the rstrip() function in Python. Let's check the code and output for the above case,

Output:

As shown in the above output, the last char 'e' is also removed.
It is because the rstrip() function checks the char from the end of the string. If the char is in the parameter, it is removed; else, the execution of rstrip() is terminated.
Consider another case where the string is "Articlegeeeezp" and we pass the string "gez" as the parameter. The output of the above code will be,

The original string and string without trailing chars are the same, as the rstrip() function was called for char 'p', and the char 'p' was not found in the parameter. Therefore further execution of the rstrip() function is terminated, and the same string is returned.

Conclusion

  • The rstrip() function in Python removes the trailing characters at the end of a string.
  • The rstrip() function has an optional parameter, chars. It is a string that specifies the trailing characters to be removed.
  • If no parameter is passed to the rstrip() function, then trailing white spaces or ' ' are removed from the end of the string.

See Also