rstrip()方法返回字符串的副本,其中删除了尾随字符(基于传递的字符串参数)。如果未传递任何参数,它将删除尾随空格。
用法:
string.rstrip([chars])
参数:
chars
(optional) – a string specifying the set of characters to be removed.
返回值:
returns a copy of the string with trailing characters stripped
代码1:
# Python3 program to demonstrate the use of
# rstrip() method using optional parameters
# string which is to be stripped
string = "geekssss"
# Removes given set of characters from
# right.
print(string.rstrip('s'))
输出:
geek
代码2:
# Python3 program to demonstrate the use of
# rstrip() method using optional parameters
# string which is to be stripped
string = " for "
# Leading whitespaces are removed
print("Geeks" + string.rstrip() + " Geeks ")
输出:
Geeks for Geeks
代码3:
# string which is to be stripped
string = "geeks for geeks"
# Argument doesn't contain trailing 's'
# So, no characters are removed
print(string.rstrip('ek'))
输出:
geeks for geeks
代码4:
# string which is to be stripped
string = "geeks for geeks"
# Removes given set of characters from
# right.
print(string.rstrip('ske'))
输出:
geeks for g
相关用法
注:本文由纯净天空筛选整理自Akanksha_Rai大神的英文原创作品 Python | String rstrip()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。