【问题标题】:How can I add a space after a specific character in a string? [duplicate]如何在字符串中的特定字符后添加空格? [复制]
【发布时间】:2017-02-10 22:23:30
【问题描述】:

我需要在字符串中的 X 之间添加一个空格。该程序在一个字段中进行测量,我需要能够在进行计算之前将整数与“x”分开。

例如:“12x24”应为“12 x 24”

【问题讨论】:

  • 那个答案甚至问题与我的相似?在我看来完全没有

标签: python string join


【解决方案1】:

使用str.replace()函数将'x'替换为'<space>x<space>'

>>> my_str = '12x24'
>>> my_str.replace('x', ' x ')
'12 x 24'

【讨论】:

  • 太好了,成功了。如此简单,但我无法想出它。谢谢。
【解决方案2】:

使用replace 方法将' x ' 替换为'x'

string.replace('x', ' x ')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    相关资源
    最近更新 更多