在 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 docs

Deprecated since version 3.5: Passing a format string as keyword argument format_string has been deprecated.

Python 3.5+ 中最好的替代品是什么?

试试这个

name = "john"
hello = "GoodMorning %s" %(name,)
print (hello)

或使用fstrings:

name = "Bob"
hello = f"Hello {name}"
print (hello)

输出:

Hello Bob

弃用是关于 string.Formatter 而不是关于 str.Formatter:

Source:

Passing a format string as keyword argument format_string to the format() method of the string.Formatter class has been deprecated.

您可以在 str.format 中使用,但不能在 string.Formatter

中使用