pythonoutputquotation-marks

How to print variable inside quotation marks?


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?


Solution

  • you can use format:

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

    Learn about format here:Format

    In 3x you can use f:

    >>> f'"{s}"'
    '"hello"'