[英]Alternative to passing a format string as keyword argument in Python 3.5+
在Python 3.5中,不推薦在str.format
使用關鍵字參數:
"Hi {s}".format(s="world")
從string
文檔 :
從版本3.5開始不推薦使用:不推薦使用格式字符串作為關鍵字參數
format_string
。
Python 3.5+中最好的替代品是什么?
棄用是關於string.Formatter
而不是關於str.Formatter
:
將格式字符串作為關鍵字參數
format_string
傳遞給string.Formatter
類的format()
方法已被棄用。
您可以在str.format
中使用,但不能在string.Formatter
或者使用fstrings
:
name = "Bob"
hello = f"Hello {name}"
print (hello)
輸出:
Hello Bob
嘗試這個
name = "john"
hello = "GoodMorning %s" %(name,)
print (hello)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.