📜  Python字符串 rstrip() 方法

📅  最后修改于: 2022-05-13 01:55:42.535000             🧑  作者: Mango

Python字符串 rstrip() 方法

Python String rstrip()方法返回删除了尾随字符的字符串副本(基于传递的字符串参数)。如果没有传递任何参数,它会删除尾随空格。

示例 1

Python3
# 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'))


Python3
# 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 ")


Python3
# 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'))


Python3
# string which is to be stripped
string = "geeks for geeks"
 
# Removes given set of characters from
# right.
print(string.rstrip('ske'))


输出:  

geek

示例 2

Python3

# 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

Python3

# 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

Python3

# string which is to be stripped
string = "geeks for geeks"
 
# Removes given set of characters from
# right.
print(string.rstrip('ske'))

输出:  

geeks for g