2013-02-02 26 views
0

我是python的新手,想打印用戶名值的雙引號,它的值在main中傳遞。我試圖把\(反斜槓)沒有幫助(使用Python 3.3)在python中打印一個變量的雙引號

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

expectd output 

</test Name="bob" Password="[email protected]" /> 

Request('test','bob','[email protected]') calling the function 
+0

出於興趣,你在哪裏嘗試把'\「's當它不工作?像'print(」這是一個\「quoted \」輸出「)'應該工作正常。 –

回答

1

你總是可以使用類似:

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

最簡單的方式做到這一點會被添加「'串裏面你函數調用是這樣的:

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

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