介绍
Python 中的 rstrip() 函数允许您删除在字符串末尾指定的字符。 如果没有指定字符,它将删除空格。 空格是函数将删除的默认字符。
这是快速删除不需要的字符的有用功能。 现在我们将向您展示如何在 Python 中使用 rstrip() 函数。
例子
txt = " cat " x = txt.rstrip() print(x)
输出:
猫
定义
rstrip() 函数删除您指定的任何字符。 它只删除末尾的字符。
空格是函数将删除的默认字符。
语法
string.rstrip(chars)
参数值:
chars:你要求删除的字符
更多例子
示例 1:
str = "pythonaaaa" x = str.rstrip('a') print(x)
输出:
Python
示例 2:
str = "cateri,." x = str.rstrip("eir,.") print(x)
输出:
猫
结论
我们刚刚向您展示了如何在 Python 中使用 rstrip() 函数。
感谢您的阅读!