Tutorial Study Image

Python rstrip()

Python 中的 rstrip() 函数根据给定的参数从原始字符串的副本中删除尾随字符。该方法将此副本作为输出返回。


string.rstrip([chars]) #where chars are those to remove from right
 

rstrip() 参数

rstrip() 函数将一组字符作为其参数。如果未提供字符,则从字符串中删除尾随空格。

参数 描述 必需/可选
chars 要删除的尾随字符 可选

rstrip() 返回值

返回值始终是字符串。直到找到第一个匹配项,所有字符组合都从右侧开始删除。

输入 返回值
字符串 字符串副本

Python 中 rstrip() 方法的示例

示例 1:rstrip() 在 Python 中如何工作?


string1 = "     python     "

string2 = string1.rstrip()

print("of all programming languages", string2, "is my favorite")
 

输出


of all programming languages     python is my favorite

示例 2:rstrip() 在 Python 中的工作原理


# Variable declaration  
string1 = "Python and Java* "  
# Calling function  
string2 = string1.rstrip(*)  
# Displaying result  
print("Before strip: ",string1)  
print("After strip: ",string2)  
 

输出


Before strip:  Python and Java* 
After strip:  Python and Java