首页题目详情
以下哪条语句是正确的使用str.format()方法的示例?

A.print('Hello, {}!'.format('world'))
B.print('Hello, {0}!'.format('world'))
C.print('Hello, {world}!'.format('world'))
D.print('Hello, {name}!'.format(name='world'))
优质解答
答案
D
解析
str.format()方法可以通过位置参数、关键字参数等进行格式化。选项D使用关键字参数正确地将变量name的值'world'插入到字符串中。
Python程序设计单选题中等AI生成