美文网首页
format方法

format方法

作者: wjv | 来源:发表于2019-05-08 15:06 被阅读0次

在python编程规范中,提出字符串格式化输出尽量用format函数而不是%。

主要包括以下几种:

1、直接用中括号表示。

"Xiaoming is {} years old and {} kg".format(18,60)

输出     'Xiaoming is 18 years old and 60 kg'

2、带上序号,可以指定位置

以下两条语句输出结果是一样的

"Xiaoming is {0} years old and {1} kg".format(18,60)

"Xiaoming is {1} years old and {0} kg".format(60,18)

'Xiaoming is 18 years old and 60 kg'

3、可以加上变量

"Xiaoming is {age} years old and {weight} kg".format(age = 18, weight = 60)

'Xiaoming is 18 years old and 60 kg'

相关文章

网友评论

      本文标题:format方法

      本文链接:https://www.haomeiwen.com/subject/ifnmoqtx.html