Python String rstrip() method
The string.rstrip() method in Python returns a copy of the string with trailing characters removed.
Syntax
string.rstrip([chars])
Parameter
chars: This argument represents a string specifying the set of characters to be removed. By default, the chars argument removes the whitespace.
Return
This method returns a copy of the string with trailing characters removed.
Example 1
1 2 3 4 5 6 7 8 |
# Python program demonstrating the use of # string.rstrip() method # string which is to be stripped string = "tutorialsssssss" # Removes the specified set of characters from the right. print(string.rstrip('s')) |
Output
1 2 3 |
tutorial |
Example 2
1 2 3 4 5 6 7 8 9 10 11 |
# Python program demonstrating the use of # string.rstrip() method # string which is to be stripped string = "Tutorials and Examples" print("Actual string...\n") # Removes given set of characters from # right. print("After striping 'sel' from the string...") print(string.rstrip('sel')) |
Output
1 2 3 4 5 6 |
Actual string... Tutorials and Examples After striping 'sel' from the string... Tutorials and Examp |
Example 3
1 2 3 4 5 6 7 8 9 10 11 12 |
# Python program explaining # the string.rstrip() method # initializing the string string = "hellloooo...,,,,,,,woorllllddddddd." print("Actual string:",string) # striping the values print("After the right striped method") val = string.rstrip(",.orld") # printing the striped value print(val) |
Output
1 2 3 4 5 |
Actual string: hellloooo...,,,,,,,woorllllddddddd. After the right striped method hellloooo...,,,,,,,w |