如何在字符串python中的每个逗号前插入问号?

如何在字符串python中的每个逗号前插入问号?,python,python-3.x,Python,Python 3.x,如何使用python在字符串中的每个逗号前插入问号? 例如,刺痛可能是 string = 'the, quick, brown, fox, jumps, over, the lazy, dog' print(string) 期望的输出是 the?, quick?, brown?, fox?, jumps?, over?, the?, lazy?, dog 试试这个: string_list = string.split(", ") new_list = [] for s

如何使用python在字符串中的每个逗号前插入问号? 例如,刺痛可能是

string = 'the, quick, brown, fox, jumps, over, the lazy, dog'
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(“,”,“?,”)

您可以这样做

string.replace(",", "?,")
print(string)
将以下列方式输出:

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

string.replace(“,”,“?,”)
这是否回答了您的问题<代码>打印(字符串)-打印上面的字符串。你能分享你的代码吗?这样我们就可以帮你更正它了?@Sushanth你不应该把你的答案写在一个真实的答案框里吗?@NoahNoahNoah,教过它会被复制,所以忽略它。