【问题标题】:Python text.replace for removing section of a string [duplicate]Python text.replace 用于删除字符串的一部分[重复]
【发布时间】:2020-12-22 16:56:36
【问题描述】:

我想看看如何在不留空白的情况下删除字符串的某个部分。

运行此代码时,它输出nice today,问题是我用空格替换字符。是否可以解决此问题,或者我应该完全使用完全不同的方法?

text = ("hello bye there ")

print(text.replace("hello", "nice").replace("there","today").replace("bye", ""))

【问题讨论】:

    标签: python string text replace


    【解决方案1】:

    你应该首先删除空格:

    text = ("hello bye there ")
    print(text.replace("hello", "nice").replace("there","today").replace(" ", '').replace("bye" , " "))
    

    输出将是:

    nice today
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-24
      • 2017-12-26
      • 2012-08-15
      • 2017-03-01
      • 2021-02-10
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      相关资源
      最近更新 更多