【问题标题】:rstrip() method for strings [duplicate]字符串的 rstrip() 方法
【发布时间】:2022-08-19 03:33:58
【问题描述】:

有人可以这样做吗?打印命令不应该打印There is a space。在这里它也跳过了,而它不应该跳过。 TIA

  • str.rstrip() 将其参数解释为要删除的字符集合。 \'queue\'.rstrip(\'eu\') 导致 \'q\'。如果您使用的是 Python 3.9+,则可以使用some_sentence.removesuffix(\'at the end\')
  • \"有人可以这样做吗?\“ 做什么, 确切地? How to Ask
  • chars 参数是一个字符串,指定要删除的字符集。(docs)。所以它不会删除字符串,而是删除它包含的所有字符。
  • 这曾经困扰过我,并且我已经使用 python 工作了很长时间。 current answer 建议 removesuffix 正是过去对我有用的解决方案,并且是推荐的方式

标签: python python-3.x string


【解决方案1】:

rstrip(characters) 方法的参数是一个字符串,将从右边删除。这意味着,some_sentence 字符串中最右边的字符是设置characters,它将被删除。直到最右边的字符不在要删除的集合中为止。

https://docs.python.org/3/library/stdtypes.html?highlight=rstrip#str.rstrip

你想要完成的事情是用.removesuffix()完成的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2013-04-13
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    相关资源
    最近更新 更多