-
如果想在print的输出或者保存文件时的文件名中添加变量,可以使用下面的方法:
Old '%s %s' % ('one', 'two') New '{} {}'.format('one', 'two') Output one two Old '%d %d' % (1, 2) New '{} {}'.format(1, 2) Output 1 2 New '{1} {0}'.format('one', 'two') Output two one
-
如果想控制小数点后的位数,
'{:06.2f}'.format(3.141592653589793) # 输出共6位,小数点后有两位 003.14
- 如果想在输出结果后再空一行,如下图效果。
可以在print(‘’)中添加n,代码如下:print("max 7 feature: {}n".format(max_n_feature)) print("max 7 values: {}".format(max_n_value))
- 如想实现其他格式输出(日期,前后添加空格,字符串截取输出等功能),请参考下面的链接:
参考链接



