Python2.6不支持format(123456L, ",")或format(123, ",")的format用法,会报下面的错误 ValueError: Unknown format code ',' for object of type 'long' ValueError: Unknown format code ',' for object of type 'int' Python2.7支持format(123456L, ",")或for
format 用法详解 不需要理会数据类型的问题,在%方法中%s只能替代字符串类型 单个参数可以多次输出,参数顺序可以不相同 填充方式十分灵活,对齐方式十分强大 官方推荐用的方式,%方式将会在后面的版本被淘汰 format填充字符串 一 填充 1.通过位置来填充字符串 print('hello {0} i am {1}'.format('world','python')) # 输入结果:hello world i am python print('hello {} i am {}'.format
format OR % 提到Python中的格式化输出方法,一般来说有以下两种方式: print('hello %s' % 'world') # hello world print('hello {}'.format('world')) # hello world 到底哪种好呢,反正对我来说,用了.format()之后就再也不想用%了. format()不用理会数据类型,%s,%f等等我记不完: format()功能更丰富,填充方式,对齐方式都很灵活,让你的打印效果更美观: format()是官