For instance, we have:
word = \'Some Random Word\'
print \'\"\' + word + \'\"\'
is there a better way to print double quotes around a varia
How about json.dumps
:
>>> import json
>>> print(json.dumps("hello world"))
"hello world"
The advantage over other approaches mentioned here is that it escapes quotes inside the string as well (take that str.format
!), always uses double quotes and is actually intended for reliable serialization (take that repr()
!):
>>> print(json.dumps('hello "world"!'))
"hello \"world\"!"