If my input text is
a b c d e f g
and I want my output text to be: (with the double quotes)
"a b c d e f g"
Where do I go after this step:
" ".join([a.strip() for a in b.split("\n") if a])Answer1:
You have successfully constructed a string without the quotes. So you need to add the double quotes. There are a few different ways to do this in Python:
>>> my_str = " ".join([a.strip() for a in b.split("\n") if a]) >>> print '"' + my_str + '"' #Use single quotes to surround the double quotes "a b c d e f g" >>> print "\"" + my_str + "\"" #Escape the double quotes "a b c d e f g" >>> print '"%s"'%my_str #Use string formatting "a b c d e f g"
Any of these options are valid and idiomatic Python. I might go with the first option myself simply because it's short and clearAnswer2: