python零基础学习笔记(十)-AttributeError: ‘list‘ object has no attribute ‘format‘解决办法

python零基础学习笔记(十)-AttributeError: 'list' object has no attribute 'format'解决办法


在python零基础学习笔记(九)中举例:

students2 = ['a', 'b', 'c', 'd']
print('new students are: ' + str(students2))
print('new students are: ' + students2.format(students2))

时,执行第二个语句结果为:
new students are: [‘a’, ‘b’, ‘c’, ‘d’]

而执行第三个语句结果报错:
AttributeError: ‘list’ object has no attribute ‘format’

这是由于列表没有format的用法。

此时去掉students2.即可,即:

print('new students are: ' + format(students2))

结果为:
new students are: [‘a’, ‘b’, ‘c’, ‘d’]
输出跟上一个语句一致

你可能感兴趣的:(python入门系列(完),python,编程语言,机器学习,人工智能,数据挖掘)