小编Rah*_*key的帖子

如何用下划线替换空格以及用字符串的空格替换下划线?

这里我用下划线替换空格,但我无法用空格替换下划线

import re

def repl(string):
    pattern = re.compile(' ')
    match = pattern.search(string)
    new_str = pattern.sub('_', string)
    print(new_str)

repl('But I want_to bring_out something_that we_are back to stay.')
Run Code Online (Sandbox Code Playgroud)

输出 :But_I_want_to_bring_out_something_that_we_are_back_to_stay.

python regex

2
推荐指数
1
解决办法
3173
查看次数

标签 统计

python ×1

regex ×1