16

I would like to print a variable within quotation marks. I want to print out "variable"

I have tried a lot, what worked was:

print('"', variable, '"')

but then I have two spaces in the output:

" variable "

How can I print something within a pair of quotation marks?

Gustavo Bezerra
  • 9,984
  • 4
  • 40
  • 48
Heiko
  • 329
  • 1
  • 2
  • 7
  • 1
    `>>> print '"variable"' "variable"` you just need one `'` – Mazdak Jan 03 '15 at 16:50
  • 3
    This is very unclear. Within quotation marks for what reason? Do you want something a shell parser or a Pythor parser can read back to the original string? – Charles Duffy Jan 03 '15 at 16:53
  • `'"'variable'"'` is appropriate for shell, not for Python. For Python, you might use `'"' + str(variable) + '"'`. – Charles Duffy Jan 03 '15 at 16:54
  • 1
    @CharlesDuffy It's clear to me what OP's problem is. He's getting unwanted spaces in a Python 2 comma-separated print list. The " after `variable` is a typo, and one that I can't fix because it's a single-character edit. – Mike Housky Jan 03 '15 at 17:17
  • @MikeHousky, if the problem is generating CSV, then this is the wrong approach entirely -- the `csv` module would be the right one, able to correctly handle (for instance) variable content containing literal quote characters. – Charles Duffy Jan 03 '15 at 17:41
  • @CharlesDuffy Not "generating CSV", but the fact that multiple argument to print (separated by commas) have spaces between the str() formatted output. OP just left out the print statement and left a stray " quote in the question. That's pretty much the only way to get his output. – Mike Housky Jan 03 '15 at 19:19

6 Answers6

34

you can use format:

>>> s='hello'
>>> print '"{}"'.format(s)
"hello"

Learn about format here:Format

In 3x you can use f:

>>> f'"{s}"'
'"hello"'
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
16

If apostrophes ("single quotes") are okay, then the easiest way is to:

print repr(str(variable))

Otherwise, prefer the .format method over the % operator (see Hackaholic's answer).

The % operator (see Bhargav Rao's answer) also works, even in Python 3 so far, but is intended to be removed in some future version.

The advantage to using repr() is that quotes within the string will be handled appropriately. If you have an apostrophe in the text, repr() will switch to "" quotes. It will always produce something that Python recognizes as a string constant.

Whether that's good for your user interface, well, that's another matter. With % or .format, you get a shorthand for the way you might have done it to begin with:

print '"' + str(variable) + '"'

...as mentioned by Charles Duffy in comment.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Mike Housky
  • 3,959
  • 1
  • 17
  • 31
  • This is the best answer, but even better would be `print repr(variable)` without the call to `str`; if `variable` is e.g. `3` or `False` then you probably don't want quotes in the output. – Jim Oldfield Jan 11 '16 at 17:27
  • @JimOldfield The `str()` call was deliberate. The caller was trying to quote whatever was in his `variable`. Besides, if you don't want to quote non-strings, wouldn't you want to print out `str(variable)` instead of `repr(variable)` whenever there was a difference? – Mike Housky Jan 17 '16 at 10:08
4

Simply do:

print '"A word that needs quotation marks"'

Or you can use a triple quoted string:

print( """ "something" """ )
Jobs
  • 3,317
  • 6
  • 26
  • 52
3

There are two simple ways to do this. The first is to just use a backslash before each quotation mark, like so:

s = "\"variable\""

The other way is, if there're double quotation marks surrounding the string, use single single quotes, and Python will recognize those as a part of the string (and vice versa):

s = '"variable"'
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
hmir
  • 1,333
  • 2
  • 17
  • 24
2

format is the best. These are alternatives.

>>> s='hello'       # Used widly in python2, might get deprecated
>>> print '"%s"'%(s)
"hello"

>>> print '"',s,'"' # Usin inbuilt , technique of print func
" hello "

>>> print '"'+s+'"' # Very old fashioned and stupid way
"hello"
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
0

you can use string Concatenation check the below example

n= "test"
print ('"' + n + '"')