python split problems I need the data output to look different

137 Views Asked by At
s = "ez , dad , tada"
print(s.split(" , "))

prints the following:

['ez', 'dad', 'tada']

The problems is, I need the output to be with double quotes, not with single quotes, like this:

["ez", "dad", "tada"]
2

There are 2 best solutions below

1
On BEST ANSWER

Credit to @fn. for this posting

Example:

import json

s = "ez , dad , tada"

print(json.dumps(s.split(" , ")))

Output:

["ez", "dad", "tada"]
2
On

You've shown that you used the following code to split up a string into an list:

s = "ez , dad , tada"

split_data = s.split(" , ")

Now that you have the data in an list, you can reformat it by using the .join() method of a string, to turn the list into a string. According to your example, you want to format the string like ["ez", "dad", "tada"], so what you can do is to merge each 2 items with ", " since that seems to be the seperating string.

separator = '", "'

print('["' + separator.join(split_data) + '"]')