簡體   English   中英

替代在Python 3.5+中傳遞格式字符串作為關鍵字參數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM