Python String rstrip() Method
The Python rstrip() method is a string method that removes any trailing characters (characters at the end of a string) from a string. It has the following syntax:
string.rstrip(characters)
where characters is an optional parameter that specifies a set of characters to be removed (default is any whitespace).
The rstrip() method returns a copy of the string with trailing characters removed.
Here are some examples of using the Python rstrip() method:
# Example 1: Remove trailing whitespace
mystr = 'Hello World '
result = mystr.rstrip()
print(result)
# Output: 'Hello World'
# Example 2: Remove trailing commas and periods
mystr = 'apple,banana,cherry,,,,.'
result = mystr.rstrip(',.')
print(result)
# Output: 'apple,banana,cherry'
# Example 3: Remove trailing '#'
mystr = 'Java and C#'
result = mystr.rstrip('#')
print(result)
# Output: 'Java and C'
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment