Python rstrip() Method
Description
Python rstrip() removes the specified characters at the end of the string, defaulting to whitespace, including spaces, newlines, carriage returns, and tabs.
Syntax
The syntax of the rstrip() method is:
str.rstrip([chars])
Parameters
- chars -- The specified characters to remove (default is whitespace)
Return Value
Returns a new string generated after removing the specified characters at the end of the string.
Example
The following example demonstrates how to use the rstrip() function:
Example
#!/usr/bin/python
# -*- coding: UTF-8 -*-
random_string = 'this is good '
# Spaces at the end of the string 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 character was found, 'm/' is removed from the string
website = 'www.example.com/'
print(website.rstrip('m/.'))
# Remove commas (,), periods (.), and the letters s, q, or w; these are all trailing characters
txt = "banana,,,,,ssqqqww....."
x = txt.rstrip(",.qsw")
print(x)
# Remove trailing character *
str = "*****this is string example....wow!!!*****"
print (str.rstrip('*'))
print(x)
# -*- coding: UTF-8 -*-
random_string = 'this is good '
# Spaces at the end of the string 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 character was found, 'm/' is removed from the string
website = 'www.example.com/'
print(website.rstrip('m/.'))
# Remove commas (,), periods (.), and the letters s, q, or w; these are all trailing characters
txt = "banana,,,,,ssqqqww....."
x = txt.rstrip(",.qsw")
print(x)
# Remove trailing character *
str = "*****this is string example....wow!!!*****"
print (str.rstrip('*'))
print(x)
The output of the above example is as follows:
this is good this is good this is g www.example.co banana *****this is string example....wow!!!
Other Extensions
Python Strings