Python 中的 rstrip() 函数根据给定的参数从原始字符串的副本中删除尾随字符。该方法将此副本作为输出返回。
string.rstrip([chars]) #where chars are those to remove from right
rstrip() 函数将一组字符作为其参数。如果未提供字符,则从字符串中删除尾随空格。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| chars | 要删除的尾随字符 | 可选 |
返回值始终是字符串。直到找到第一个匹配项,所有字符组合都从右侧开始删除。
| 输入 | 返回值 |
|---|---|
| 字符串 | 字符串副本 |
string1 = " python "
string2 = string1.rstrip()
print("of all programming languages", string2, "is my favorite")
输出
of all programming languages python is my favorite
# 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