繁体   English   中英

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

[英]print double quotes around a variable in python

我是python的新手,想在用户名的值周围打印双引号,它的值在主要传递。 我试图把\\(反斜杠)没有帮助(使用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是替换变量的首选方式。

您还可以使用命名值:

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

还有更多的方法可以很好地格式化字符串。

查看http://docs.python.org/2/library/stdtypes.html#str.format了解更多信息。

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

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

最简单的方法是在函数调用中添加'“'字符串,如下所示:

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

暂无
暂无

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

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