提问者:小点点

如何在字符串Python中的每个逗号前插入问号? [已关闭]


如何用Python在字符串中的每个逗号前插入问号? 例如,刺痛将是

string = 'the, quick, brown, fox, jumps, over, the lazy, dog'
print(string)

所需的输出为

the?, quick?, brown?, fox?, jumps?, over?, the?, lazy?, dog

共2个答案

匿名用户

你可以这样做

string.replace(",", "?,")
print(string)

将输出为:

'the?, quick?, brown?, fox?, jumps?, over?, the?, lazy?, dog'

匿名用户

请尝试以下操作:

string_list = string.split(", ")

new_list = []
for string in string_list:
    new_list.append(string + "?")
final_string = ", ".join(new_list)

或者这样:new_string=string.replace(“,”,“?,”)