【发布时间】:2022-01-30 18:01:17
【问题描述】:
我需要替换给定句子中的空格而不替换前两个单词中的空格,有没有办法在 python 中做到这一点?
【问题讨论】:
-
提供样本数据和所需的输出,以及您尝试过的内容
我需要替换给定句子中的空格而不替换前两个单词中的空格,有没有办法在 python 中做到这一点?
【问题讨论】:
如果您发布一个示例,它会更容易回答,但这里是您所要求的猜测
# Slicing method
s = "Word one Word two rest of sentence"
s = s[:17] + s[17:].replace(' ', '_')
# result: 'Word one Word two_rest_of_sentence'
【讨论】: