【问题标题】:In Python I want to replace <phrase>the cow jumped over the moon</phrase> with <phrase>the_cow_jumped_over_the_moon</phrase> [duplicate]在 Python 中,我想用 <phrase>the_cow_jumped_over_the_moon</phrase> 替换 <phrase>the cow jumped over the moon</phrase> [重复]
【发布时间】:2020-02-26 05:45:50
【问题描述】:
在 Python 中,我想用 <phrase>the_cow_jumped_over_the_moon</phrase> 替换 <phrase>the cow jumped over the moon</phrase>
所以只有在<phrase> 和</phrase> 内的空格应该替换为_,它是一个下划线
所有其他地方不应该改变
【问题讨论】:
标签:
python
string
replace
【解决方案1】:
请使用替换方法完成您的任务
msg1 = "the cow jumped over the moon"
print(str(msg1).replace(" ","_"))
输出:
the_cow_jumped_over_the_moon