我是新来这里和Python以及,但我想给它一个尝试! 我想在句子中用“top”替换“t”,用“hop”替换“h”,只有当“th”不可见时,因为“th”将变成“thop”。例如:“Thi hi tea”必须变成“thopi hopi topea”。 我有这个密码:
sentence = str(raw_input('give me a sentence '))
start = 0
out = ''
while True:
i = string.find( sentence, 'th', start )
if i == -1:
sentence = sentence.replace('t', 'top')
sentence = sentence.replace('h', 'hop')
break
out = out + sentence[start:i] + 'thop'
start = i+2
但不起作用…有什么想法吗?你知道吗
收益率
为了打破它
import re
导入正则表达式库,我们从中使用替换函数sub
(?i)
使regex大小写不敏感t(?!h)
匹配“t”而不是后跟“h”\g<0>op
是替换字符串,用"op"
替换原始文本。你知道吗函数如下:
回答:
谢谢
相关问题 更多 >
编程相关推荐