Reputation: 77
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
Reputation: 87
You can use str.format()
print ('Hostname="{}" IP="{}" tags="{}"' .format(name,value["ip"],tags) )
Upvotes: 2
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
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
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