格式化字符串时尽量使用format()方式而不是%
Python中内置的 %操作符 和 format()方式 都可以用于格式化字符串。 使用字符串格式设置运算符——百分号 %操作符 根据转换说明符所规定的格式返回一串格式化后的字符串,转换说明符的基本形式为: %[转换标记][宽度[.精确度]]转换类型 。 %操作符格式化字符串时有如下几种常见用法: 直接格式化字符或者数值 以元组的形式格式化 以字典的形式格式化 print ( "The %s is a kind of %s." % ( 'apple' , 'fruit' ) ) # The apple is a kind of fruit. person = { "name" : "watkins" , "address" : "siso" } print ( "The address of %(name)s is %(address)s." % person ) # The address of watkins is siso. 对字符串调用方法format print ( "The age of {name} is {age:02d}" . format ( name = 'watkins' , age = 5 ) ) # The age of watkins is 05 format()方法 格式字符串的调用格式为: "{字段名!转换标志:格式说明符}.format()"