'Insert commas between words in a string in Python
I just want to put commas as separators of words in a Python string, in which each word is enclosed by quotes. My input can be: "some" "words" "but" "no" "commas" and I would like to have a string like this: "some", "words", "but", "no", "commas"
so the last word will not have the comma at its end. Thank you in advance!
Solution 1:[1]
txt = '"some" "words" "but" "no" "commas"'
commas_added = ', '.join(txt.split())
Solution 2:[2]
Are you expecting something like this? (python 2.7.5)
input1 = "some"
input2 = "words"
output = "\"" + input1 + "\",\"" + input2 + "\""
output
'"some","words"'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gabriel |
Solution 2 |