2015-10-17 81 views
1

如何在「」中打印變量?例如, ;如何在「」內打印變量?

a = "test" 
print"this is a (how do I input 'a' here?)" 
+0

使用'打印 「這是(我怎麼輸入 '%s' 的嗎?)」 %了' –

+0

@TamilSelvan運算使用Python 3.x的 –

+0

@AvinashRaj @idjaw你是對的。 python 3有不同的語法。嘗試'print(「這是一個(我怎麼輸入」,a,「在這裏?)」) –

回答

4

你應該閱讀打印tutorial

但你在找什麼做的是這樣的:

print("this is a {} here?".format(a)) 

如果您正在尋找把多個變量在你的字符串,你可以簡單地做到這一點:

print("this is a {0} here and {1} and also {2}?".format(a, b, c)) 

你實際上可以寫這樣的線這樣witho UT斯達康數量規格:

print("this is a {} here and {} and also {}?".format(a, b, c)) 

作爲明確與數字將是最好的,如果你正在尋找重新排序的參數放置在字符串中,像這樣的:

print("this is a {2} here and {0} and also {1}?".format(a, b, c)) 

所以,上面的例子中,第一個字符串是c,然後是a,然後是b。

對於您試圖在字符串中添加引號的特定情況,可以採用不同的方法來執行此操作。如果你想雙引號,你可以用單引號包:

print('this is a "{2}" here and "{0}" and also "{1}"?'.format(a, b, c)) 

如果你想單引號,換用雙引號:

print("this is a '{2}' here and '{0}' and also '{1}'?".format(a, b, c)) 

最後,還有三單引號:

print('''this is a '{2}' here and '{0}' and also '{1}'?'''.format(a, b, c)) 

混合取其類型的報價:

print('''this is a "{2}" here and "{0}" and also '{1}'?'''.format(a, b, c)) 
+0

除非重新排序用法,或者多次重複使用同一個參數,否則不需要顯式編號。第二個例子也可以是三個用法。 – ShadowRanger

+0

是的,這是正確的。爲了這個例子,我只是想更加明確。我會記下這個艱難的。謝謝。 – idjaw