Python: enclose each word of a space separated string in quotes
Question:
I have a string eg:
line = "a sentence with a few words"
I want to convert the above in a string with each of the words in double quotes, eg:
'"a" "sentence" "with" "a" "few" "words"'
Any suggestions?
Answers:
Split the line into words, wrap each word in quotes, then re-join:
' '.join('"{}"'.format(word) for word in line.split(' '))
Since you say:
I want to convert the above in a string with each of the words in double quotes
You can use the following regex:
>>> line="a sentence with a few words"
>>> import re
>>> re.sub(r'(w+)',r'"1"',line)
'"a" "sentence" "with" "a" "few" "words"'
This would take into consideration punctuations, etc, as well (if that is really what you wanted):
>>> line="a sentence with a few words. And, lots of punctuations!"
>>> re.sub(r'(w+)',r'"1"',line)
'"a" "sentence" "with" "a" "few" "words". "And", "lots" "of" "punctuations"!'
Or you can something simpler (more implementation but easier for beginners) by searching for each space in the quote then slice whatever between the spaces, add ” before and after it then print it.
quote = "they stumble who run fast"
first_space = 0
last_space = quote.find(" ")
while last_space != -1:
print(""" + quote[first_space:last_space] + """)
first_space = last_space + 1
last_space = quote.find(" ",last_space + 1)
Above code will output for you the following:
"they"
"stumble"
"who"
"run"
The first answer missed an instance of the original quote. The last string/word “fast” was not printed.
This solution will print the last string:
quote = "they stumble who run fast"
start = 0
location = quote.find(" ")
while location >=0:
index_word = quote[start:location]
print(index_word)
start = location + 1
location = quote.find(" ", location + 1)
#this runs outside the While Loop, will print the final word
index_word = quote[start:]
print(index_word)
This is the result:
they
stumble
who
run
fast
I have a string eg:
line = "a sentence with a few words"
I want to convert the above in a string with each of the words in double quotes, eg:
'"a" "sentence" "with" "a" "few" "words"'
Any suggestions?
Split the line into words, wrap each word in quotes, then re-join:
' '.join('"{}"'.format(word) for word in line.split(' '))
Since you say:
I want to convert the above in a string with each of the words in double quotes
You can use the following regex:
>>> line="a sentence with a few words"
>>> import re
>>> re.sub(r'(w+)',r'"1"',line)
'"a" "sentence" "with" "a" "few" "words"'
This would take into consideration punctuations, etc, as well (if that is really what you wanted):
>>> line="a sentence with a few words. And, lots of punctuations!"
>>> re.sub(r'(w+)',r'"1"',line)
'"a" "sentence" "with" "a" "few" "words". "And", "lots" "of" "punctuations"!'
Or you can something simpler (more implementation but easier for beginners) by searching for each space in the quote then slice whatever between the spaces, add ” before and after it then print it.
quote = "they stumble who run fast"
first_space = 0
last_space = quote.find(" ")
while last_space != -1:
print(""" + quote[first_space:last_space] + """)
first_space = last_space + 1
last_space = quote.find(" ",last_space + 1)
Above code will output for you the following:
"they"
"stumble"
"who"
"run"
The first answer missed an instance of the original quote. The last string/word “fast” was not printed.
This solution will print the last string:
quote = "they stumble who run fast"
start = 0
location = quote.find(" ")
while location >=0:
index_word = quote[start:location]
print(index_word)
start = location + 1
location = quote.find(" ", location + 1)
#this runs outside the While Loop, will print the final word
index_word = quote[start:]
print(index_word)
This is the result:
they
stumble
who
run
fast