How to print variable inside quotation marks?

Heiko picture Heiko · Jan 3, 2015 · Viewed 66.4k times · Source

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

I have tried a lot, what worked was: '"', variable", '"' – but then I have two spaces in the output -> " variable "

When I do print '"'variable'"' without the comma I get a syntax error.

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

Answer

Hackaholic picture Hackaholic · Jan 3, 2015

you can use format:

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

Learn about format here:Format

In 3x you can use f:

>>> print(f"{s}")
hello