Python3 rstrip() Method
Description
The rstrip() method removes specified characters from the end of a string. By default, it removes whitespace characters, including spaces, newlines, carriage returns, and tabs.
Syntax
The syntax for the rstrip() method is:
str.rstrip()
Parameters
- chars -- Specifies the characters to be removed (default is whitespace characters).
Return Value
Returns a new string with the specified characters removed from the end of the original string.
Examples
The following examples demonstrate the usage of the rstrip() function:
Example
#!/usr/bin/python3
random_string = 'this is good '
# Trailing spaces will be removed
print(random_string.rstrip())
# 'si oo' are not trailing characters, so nothing is removed
print(random_string.rstrip('si oo'))
# In 'sid oo', 'd oo' are trailing characters, 'ood' is removed from the string
print(random_string.rstrip('sid oo'))
# 'm/' are trailing characters, no trailing '.' found, 'm/' is removed from the string
website = 'www./'
print(website.rstrip('m/.'))
# Remove comma (,), period (.), letters s, q, or w, as they are all trailing characters
txt = "banana,,,,,ssqqqww....."
x = txt.rstrip(",.qsw")
print(x)
# Remove trailing asterisks (*)
str = "*****this is string example....wow!!!*****"
print(str.rstrip('*'))
The output of the above examples is as follows:
this is good
this is good
this is g
www.tutorial.co
banana
***** this is string example....wow!!!
YouTip