在Python3.5+;中作为关键字参数传递格式字符串的替代方法;
在Python 3.5中,不推荐在在Python3.5+;中作为关键字参数传递格式字符串的替代方法;,python,string,string-formatting,python-3.5,Python,String,String Formatting,Python 3.5,在Python 3.5中,不推荐在str.format中使用关键字参数: "Hi {s}".format(s="world") 从: 自3.5版以来已弃用:已弃用将格式字符串作为关键字参数传递format\u string Python 3.5+中最好的替代方案是什么?试试这个 name = "john" hello = "GoodMorning %s" %(name,) print (hello) 或使用fstrings: name = "Bob" hello = f"Hello {nam
str.format
中使用关键字参数:
"Hi {s}".format(s="world")
从:
自3.5版以来已弃用:已弃用将格式字符串作为关键字参数传递format\u string
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
:
将格式字符串作为关键字参数format\u string
传递给string.Formatter
类的format()
方法已被弃用
您可以在
str.format
中使用,但不能在string.Formatter
中使用。请修复弃用通知链接感谢您的澄清-我误解了弃用警告。所以我想使用“Hi{s}.format(s=“world”)
继续前进是很好的吗?@joshlk,是的,很好。