温馨提示×

如何正确使用Python rstrip函数

小樊
84
2024-07-25 03:42:09
栏目: 编程语言

Python中的rstrip函数用于删除字符串末尾的指定字符(默认为空格)或字符序列。下面是如何正确使用Python的rstrip函数的示例:

  1. 删除末尾的空格符:
s = "hello world    "
s = s.rstrip()
print(s)  # 输出: "hello world"
  1. 删除末尾的特定字符:
s = "hello world 123"
s = s.rstrip("123")
print(s)  # 输出: "hello world "
  1. 删除末尾的字符序列:
s = "hello world123"
s = s.rstrip("123")
print(s)  # 输出: "hello world"

需要注意的是,rstrip函数只会删除字符串末尾的字符,而不会影响字符串中间的字符。

0