是把它推薦給格式化字符串:Python3打印字符串格式化
var = 'python 3'
print(f"This is {var}") # prints out This is python 3
或
var = 'python 3'
print("This is", var)
或舊的字符串格式化
var = 'python 3'
print("This is %s" % var)
而且,是print(f"{var}")
新功能python 3.6?在python 3.5.3中運行相同的代碼時,我得到了SyntaxError: invalid syntax
。
是,[F-字符串(https://www.python.org/dev/peps/pep-0498 /)在3.6中是新增的 – khelwood
*「它是python 3.6的一個新功能嗎?」* - [yes](https://docs.python.org/3.6/whatsnew/3.6的.html#PEP-498格式的字符串,文字)。 – jonrsharpe
當我在3.5.2中使用[在線編譯器](https://repl.it/languages/python3)運行它時,輸出打印時沒有錯誤,所以我很困惑。 – Ricek