【发布时间】:2019-07-12 00:12:12
【问题描述】:
我怎样才能像这样在空格中添加字符/数字:
Today the ---- is cloudy, but there is no ----.
Today the --a)-- is cloudy, but there is no --b)--.(desired result)
如您所见,空格不会被固定字符替换,这使得使用 python replace() 方法对我来说很复杂。
【问题讨论】:
-
您不是在替换空格,而是在替换一串连字符。
-
你可以使用 for 循环和 if 语句来做到这一点
-
字符串是不可变的,所以你需要创建新的。也许您可以简单地使用 f 字符串:
a = "--a)--"; b = "--b)--"; f"Today the {a} is cloudy, but there is no {b}." -
总是连续的字母,
a,b,c等? -
@Alexander 想必要修改的字符串是动态的,而不是文字。
标签: python string replace text-manipulation