簡體   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