python 格式化输出

0x00

你喜欢哪一种方式进行格式化输出?

0x01

  • %s
1
2
In [1]: "hello %s" % "world"
Out[1]: 'hello world'

参数化(不知道这样叫合不合适)

1
2
3
In [2]: vari = dict(name="ronething",title="python format")
In [3]: "for %(name)s %(title)s" % vari
Out[3]: 'for ronething python format'
  • format()
1
2
In [7]: 'hello {}'.format('world')
Out[7]: 'hello world'
1
2
In [10]: "for {name} {title}".format(**vari)
Out[10]: 'for ronething python format'

索引

1
2
In [11]: "{0} {2} {1}".format("python","java","golang")
Out[11]: 'python golang java'
  • F'' or f''
1
2
3
In [13]: welcome = "hello world"                  
In [14]: F"for {welcome}"
Out[14]: 'for hello world'

0x02

封面出处:https://www.pixiv.net/member_illust.php?mode=medium&illust_id=72798502

Author: ronething
Link: https://blog.ronething.cn/20190525-python-format.html
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.