美文网首页
python 格式化输出

python 格式化输出

作者: 秋殇灬 | 来源:发表于2018-05-03 11:19 被阅读0次

1.打印字符串

>>> print "I'm %s" % ("jihite")
I'm jihite

2.打印整数

>>> print "I'm %d years old" % (17)
I'm 17 years old

3.打印浮点数

>>> print "π=%f" % (3.1415926)
π=3.141593

4.打印浮点数(指定保留小数点位数)

>>> print "π=%.3f" % (3.1415926)
π=3.142

5.指定占位符宽度

>>> print "NAME:%8s AGE:%8d             WEIGHT:%8.2f" % ("jihite", 17, 62.2)
NAME:  jihite AGE:      17 WEIGHT:   62.20

6.指定占位符宽度(左对齐)

>>> print "NAME:%-8s AGE:%-8d     WEIGHT:%-8.2f" % ("jihite", 17, 62.2)
NAME:jihite   AGE:17       WEIGHT:62.20 

7.指定占位符(只能用0当占位符)

>>> print "NAME:%-8s AGE:%08d WEIGHT:%08.2f" % ("jihite", 17, 62.2)
NAME:jihite   AGE:00000017     WEIGHT:00062.20

8.科学计数法

>>> format(0.0000023, '.2e')
'2.30e-06'
>>> format(0.23, '.2e')
'2.30e-01'

相关文章

  • 实战

    python的格式化输出 #python格式化输出 ##%对于未知变量类型,用这样就不太方便了 name='lis...

  • 入门输入输出篇

    python 的输入和输出 输出 print('hello') 格式化输出: 命令行: >>> 'Hello, %...

  • Python2与Python3中print用法总结

    Python2中的print用法 在Python2 中 print 是一种输出语句 1.格式化输出整数 2.格式化...

  • Python 中的常见 格式化符号

    Python 认识格式化输出 中的 格式化符号 在前面的文章里我们早早就接触过Python中的输出的函数prinn...

  • python—输入与输出

    Python的格式化输出 使用字符串格式化的形式来优化Python的输出,使用%作为占位符。%后面跟的是变量的类型...

  • Python使用format与f-string数字格式化

    Python使用format与f-string数字格式化 输出:

  • python笔记

    Python format格式化输出 浅谈 Python 的 with 语句 Python中迭代原理生成器和迭代原...

  • python-print函数的使用

    1.格式化输出 看看《Python基础编程》中对格式化输出的总结: %字符:标记转换说明符的开始 转换标志:-表示...

  • Python自学笔记Day9

    Python自学笔记——Day9 基本输入输出 1. 输出函数及格式化 Python两种输出值的方式: 表达式语句...

  • Python几种格式化输出

    Python格式化输出 print(userInfo)print(userInfo1)print(userInfo...

网友评论

      本文标题:python 格式化输出

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