-6

how do I insert a question mark before every comma in a string with python? for example, the sting would be

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

and the desired output would be

the?, quick?, brown?, fox?, jumps?, over?, the?, lazy?, dog
Tom Burn
  • 25
  • 4

2 Answers2

3

you could do this

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

would output in:

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

Try this:

string_list = string.split(", ")

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

Or this: new_string = string.replace(", ", "?, ")

Krishnan Shankar
  • 780
  • 9
  • 29