简体   繁体   English

在python中的变量周围打印双引号

[英]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. 我是python的新手,想在用户名的值周围打印双引号,它的值在主要传递。 i tried putting \\ (backslash) didnt help (using python 3.3) 我试图把\\(反斜杠)没有帮助(使用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

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

String.format is the preferred way of substituting variables in, nowadays. 如今,String.format是替换变量的首选方式。

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. 查看http://docs.python.org/2/library/stdtypes.html#str.format了解更多信息。

您可以始终使用类似以下内容的东西:

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

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 + '"' +'/>') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM