python字符串format()函数报错

发布于 2022-09-17  235 次阅读


额,其实我会用
但是!我还是要试一下这几个冲突是什么效果
:)

"""关键字参数必须跟随在位置参数后面"""
>>> "我是{name},我的室友是{0}".format(name="whc","👶")
  File "<stdin>", line 1
                                            ^
SyntaxError: positional argument follows keyword argument
"""手动自动编号混用错误
学习字符串format()函数,执行如下语句时报错:ValueError: cannot switch from manual field specification to automatic field numbering,意思为:值错误:无法从手动字段规范切换到自动字段编号
原来是因为自动编号和手动编号不能同时使用,所以我在最后一个花括号中填写数值后,再次执行,执行成功"""
``>>> "{1}会{}发".format("G","和")``

``Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot switch from manual field specification to automatic field numbering``

"""对应序号超出范围"""
>>> "我叫{j},我室友是{1}".format("wpl",j="whc")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: Replacement index 1 out of range for positional args tuple
```零位有,一位没有值,报错

王某人拜大佬