【问题标题】: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 中,我想用 &lt;phrase&gt;the_cow_jumped_over_the_moon&lt;/phrase&gt; 替换 &lt;phrase&gt;the cow jumped over the moon&lt;/phrase&gt; 所以只有在&lt;phrase&gt;&lt;/phrase&gt; 内的空格应该替换为_,它是一个下划线

所有其他地方不应该改变

【问题讨论】:

标签: python string replace


【解决方案1】:

请使用替换方法完成您的任务

msg1 = "the cow jumped over the moon"
print(str(msg1).replace(" ","_"))

输出:

the_cow_jumped_over_the_moon

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-06
    • 2022-01-14
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多