In this tutorial, we’ll learn what Python string rstrip() method is and how to properly use it. We’ll go through multiple Python code examples to understand how rstrip() method works.
Introduction: Python String rstrip() Method
This method returns a string after removing the trailing characters from it and its based on the string passed as an argument to this method.
Syntax of rstrip() Method
string.rstrip(str(optional))
- Python string rstrip method supports a string parameter.
- This string parameter is optional and it specifies the characters to be removed from the right side of string.
- If no argument is passed to rstrip method then only the whitespaces from right are removed from string.
- Python string rstrip method return a new string with trailing characters stripped(only whitespaces will be removed from right if no argument is passed).
Example 1: rstrip() Method with no Parameter
print('Python'.rstrip()) print(' Python Language '.rstrip()) only removes whitespaces from right(end) print(' Python Language '.rstrip()) It will removes whitespaces from right side(end) only print(' Python Programming Language.'.rstrip())
Output
Python Python Language Python Language Python Programming Language.
Python string rstrip() method remove whitespaces from right when no string argument is passed to it and can be seen in the above examples.
Example 2: rstrip() Method with String Parameter
print(' rpPython'.rstrip('r')) // trailing has no r character so nothing will be removed print(' rpPython '.rstrip(' n')) // n removed from trailing and also whitespace is defined(both in string and argument) print('rrpPython '.rstrip('thon ')) // thon will be removed from trailing print('Python'.rstrip('hn ')) // only n will be removed as o(not defined in string argument) comes after h print('Python language'.rstrip(' age')) // age(characters) will be removed from end of string print(' Python programming'.rstrip('ig')) // g will be removed print(' rpPython3'.rstrip(' thn4')) // nothing will be removed(only whitespace will be removed) as 4 comes in the very last and is not specified in argument string print(' rpPython'.rstrip('n ')) // n will be removed print(' rpPython'.rstrip('tho')) // nothing will be removed print(' rpPython '.rstrip('rpPython')) // nothing will be removed as whitespace is not defined in string argument print(' rpPython '.rstrip(' no')) // on will be removed
Output
rpPython rpPytho rrpPy Pytho Python langu Python programmin rpPython3 rpPytho rpPython rpPython rpPyth
You can try it with even more examples to better understand how Python string rstrip() method works.
Example 3: rstrip() Method Return Value
val=' Python Programming Language ' returnedVal=val.rstrip('grng Language') print(returnedVal)
Output
Python Programmi
Python string rstrip method return a new string with all the matching specified characters(passed as a string argument to rstrip method) stripped from trailing.
Click here to learn how Python string lstrip() method works.
Conclusion
To conclude this tutorial, hope you now have a detailed practical understanding of how to properly use Python string rstrip method. I’d be very delighted to have your feedback on this post. Thank you for reading it.