Sandeep Krishnan
Sandeep Krishnan

Reputation: 465

print double quotes around a variable in python

I am new to python want to print double quotes around the value of username, its value is passed in the main. i tried putting \ (backslash) didnt help (using python 3.3)

def Request(method,username,password):
  print ('</'+method+ 'Name='+username+ ' ' +'Password='+password+'/>') 

expectd output

</test Name="bob" Password="bob@123" />

 Request('test','bob','bob@123') calling the function

Upvotes: 0

Views: 4751

Answers (3)

John Brodie
John Brodie

Reputation: 6019

print('</{0} Name="{1}" Password="{2}"/>'.format(method, username, password))

String.format is the preferred way of substituting variables in, nowadays.

You could also use named values:

print('</{method} Name="{username}" Password="{password}/>'.format(method=method, username=username, password=password))

There's even more ways to nicely format strings.

Check out http://docs.python.org/2/library/stdtypes.html#str.format for more info.

Upvotes: 2

user1810455
user1810455

Reputation: 11

The easiest way to do that would be adding '"' strings inside your function call like this:

def Request(username,password):
    print ('</' + 'Name=' + '"' + username + '"' + ' ' + 'Password=' + '"' + password + '"' +'/>') 

Upvotes: 0

Laurent
Laurent

Reputation: 940

You could always use something like:

'</%s Name="%s" Password="%s" />' % (method, username, password)

Upvotes: 2

Related Questions