如何用Python在字符串中的每个逗号前插入问号? 例如,刺痛将是
string = 'the, quick, brown, fox, jumps, over, the lazy, dog'
print(string)
所需的输出为
the?, quick?, brown?, fox?, jumps?, over?, the?, lazy?, dog
你可以这样做
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(“,”,“?,”)