30

For instance, we have:

word = 'Some Random Word'
print '"' + word + '"'

Is there a better way to print double quotes around a variable?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Hero Stradivari
  • 565
  • 1
  • 6
  • 13

7 Answers7

56

Update :

From Python 3.6, you can use f-strings

>>> print(f'"{word}"')
"Some Random Word"

Original Answer :

You can try %-formatting

>>> print('"%s"' % word)
"Some Random Word"

OR str.format

>>> print('"{}"'.format(word))
"Some Random Word"

OR escape the quote character with \

>>> print("\"%s\"" % word)
"Some Random Word"

And, if the double-quotes is not a restriction (i.e. single-quotes would do)

>>> from pprint import pprint, pformat
>>> print(pformat(word))
'Some Random Word'
>>> pprint(word)
'Some Random Word'

OR like others have already said (include it in your declaration)

>>> word = '"Some Random Word"'
>>> print(word)
"Some Random Word"

Use whichever you feel to be better or less confusing.

And, if you need to do it for multiple words, you might as well create a function

def double_quote(word):
    return '"%s"' % word

print(double_quote(word), double_quote(word2))

And (if you know what you're doing &) if you're concerned about performance of these, see this comparison.

shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
  • 1
    None of these solutions (except maybe `pformat`?) handle the possibility of `word` containing a double quote. – celticminstrel Jun 21 '21 at 14:43
  • @celticminstrel that's not true. *All* of these can entertain the possibility of `word` itself having double quotes. The content of `word` does not affect the ability of the methods to add double quotes around it. If someone does want only one pair of surrounding double quotes, then that's a somewhat different ask altogether. – shad0w_wa1k3r Jun 21 '21 at 18:53
  • 2
    I guess I wasn't clear enough… I'm referring to escaping the quotes if necessary. If you don't care about that, then sure, these methods are all fine. – celticminstrel Jun 25 '21 at 18:36
  • @celticminstrel I still don't understand why escaping the quotes would be required or may mess up wrapping the `word` around them. Could you please give a sample input & expected output? – shad0w_wa1k3r Jun 25 '21 at 22:15
  • 2
    Well, it depends on why you want to do this in the first place. If it's just for output, to be displayed to the user, then it likely doesn't matter (though having a random double quote in the middle would still be weird). On the other hand, if it's meant to be serialization, then escaping the quotes is essential. These are just examples of course. – celticminstrel Sep 20 '21 at 14:39
  • Got it. Thanks. In that case one must use corresponding serializer library methods, like `json.dumps`, etc. – shad0w_wa1k3r Sep 21 '21 at 11:22
25

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\"!"
coldfix
  • 6,604
  • 3
  • 40
  • 50
11

You can try repr

Code:

word = "This is a random text" print repr(word)

Output:

'This is a random text'

Sunny Shukla
  • 342
  • 2
  • 8
6

It seems silly, but works fine to me. It's easy to read.

word = "Some Random Word"
quotes = '"'
print quotes + word + quotes
Marcelo
  • 438
  • 5
  • 16
4
word = '"Some Random Word"' # <-- did you try this?
qwertynl
  • 3,912
  • 1
  • 21
  • 43
4

Using format method or f-string with repr(), you can write it more elegant.

a = "foo"
print("{!r}".format(a))
b = "bar"
print(f"{b!r}")
watsn zhein
  • 81
  • 1
  • 2
-4

Use escape sequence

Example:

int x = 10;
System.out.println("\"" + x + "\"");

O/P

"10"
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Adarsh J
  • 61
  • 6