Python String rstrip()

Python String rstrip()

The rstrip() method returns a copy of the string with trailing characters removed (based on the string argument passed).

The rstrip() removes characters from the right based on the argument (a string specifying the set of characters to be removed).

The syntax of rstrip() is:

string.rstrip([chars])

rstrip() Parameters

  • chars (optional) - a string specifying the set of characters to be removed.

If chars argument is not provided, all whitespaces on the right are removed from the string.


Return Value from rstrip() 

The rstrip() returns a copy of the string with trailing characters stripped.

All combinations of characters in chars argument are removed from the right of the string until first mismatch.


Example: Working of rstrip()

random_string = ' this is good'

# Leading whitepsace are removed
print(random_string.rstrip())

# Argument doesn't contain 'd'
# No characters are removed.
print(random_string.rstrip('si oo'))

print(random_string.rstrip('sid oo'))

website = 'www.programiz.com/'
print(website.rstrip('m/.'))

Output

 this is good
 this is good
 this is g
www.programiz.co