Menu Close

Python String rstrip() Method

Python string rstrip

In this article, you will learn about the Python string rstrip() method along with some examples. rstrip function in Python is the best string function to remove trailing characters from the right of the string.

In the previous tutorial, we have been seen the Python string rjust() method right align the string.

Python string rstrip() method

Python rstrip() string method removes all the trailing characters ( end of the string ) from the string . It takes a char type parameter which is optional.
If a parameter is not provided then it removes all the spaces from the string.

Syntax

The syntax of Python string rstrip() method is:-

string.rstrip(characters)

Parameter

string rstrip function in Python support one argument that is character.

  • characters– ( Optional ) A set of characters to remove as trailing characters.

Return Value

Python string rstrip method return new string.

Python string rstrip example

Here we will take some example to understand rstrip function in Python.

Example 1:

a = 'PROGRAMMING     '
result = a.rstrip()
print(result)

Output will be:- PROGRAMMING

Example 2:

a = '#####--&&Programming$$$'
result = a.rstrip('$')
print(result)

Output

#####--&&Programming

Conclusion

In this python string method tutorial, you have learned the Python string rstrip method to remove all the trailing characters from the right of the string.

If you want to remove all the spaces or any trainling characters from the right of the string, Then you can go through rstrip function.

I hope this tutorial, this tutorial will help you. if you like this article, please share it with your friends who want to learn Python programming.

Other string methods


For More Information:- Click Here

Python String rjust() Method
Python String splitlines() Method

Related Posts