Aswin Ramanath
Aswin Ramanath

Reputation: 77

Printing multiple variables inside quotes in python

I am trying to print multiple variables in a print statement but I want all the variables to be printed in double quotes

While using normal formatting I am able to get it printed without the double quotes (%s) but unable to get it printed inside the double quotes while using the statement as below

print "Hostname='"%s"' IP='"%s"' tags='"%s"'"%(name,value["ip"],tags)

Upvotes: 1

Views: 2328

Answers (4)

Shreyas Khadke
Shreyas Khadke

Reputation: 87

You can use str.format()

print ('Hostname="{}" IP="{}" tags="{}"' .format(name,value["ip"],tags) )  

Upvotes: 2

Alex Martian
Alex Martian

Reputation: 3782

You can either escape with \ or use single quotes and qouble inside. See tutorial here:

http://www.pitt.edu/~naraehan/python2/tutorial7.html

>>> print "\"hello\""  
"hello"  
>>> print '"\\" is the backslash'   # Try with "\" instead of "\\"  
"\" is the backslash

Upvotes: 0

j-i-l
j-i-l

Reputation: 10957

Either go with MrGeek's answer or you can simply escape the double quotes:

print "Hostname=\"%s\" IP=\"%s\" tags=\"%s\"" % (name, value["ip"], tags)

Either way will solve your problem.

Upvotes: 2

Djaouad
Djaouad

Reputation: 22766

You should enclose the entire string with single quotes (') and each %s with double quotes ("):

print 'Hostname="%s" IP="%s" tags="%s"' % (name, value["ip"], tags)

Upvotes: 4

Related Questions