Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3.5+;中作为关键字参数传递格式字符串的替代方法;_Python_String_String Formatting_Python 3.5 - Fatal编程技术网

在Python3.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

在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 {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,是的,很好。