当前位置:   article > 正文

python格式化字符串_python help(formatting)

python help(formatting)

使用str.format()或者str%()来实现格式化字符串。

  1. 一般用法
>>> name_v = '小黑'
>>> title_v = '笨蛋'
>>> string1 = '{name}是最大的{title}'.format(name=name_v, title=title_v) 
>>> string2 = '%s是最大的%s'%(name_v, title_v)
>>> print(string1)
小黑是最大的笨蛋
>>> print(string2)
小黑是最大的笨蛋
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意:如果格式化的字符串中本身有{},需要变成{{}}

>>> string = '{{}}被用来表示一个{}'.format('参数')
>>> print(string)
{}被用来表示一个参数
  • 1
  • 2
  • 3

建议 :在字符串本身包含较多 { 或 } 时可以使用str%()格式

  1. 格式化
# 冒号(:)右边的内容表示显示格式
# >表示右对齐
# 0表示填充符号
# 3表示最小输出宽度为3位
>>> '{:>03}'.format(5)
'005'
# <表示右对齐
>>> '{:<03}'.format(5)
'500'
# 默认空格填充
>>> '{:<3}'.format(5)
'5  '
# 保留两位有效数字(只用于浮点数)
>>> '{:.2}'.format(554.45488)
'5.5e+02'
# 保留两位小数
>>> '{:.2f}'.format(554.45488)
'554.45'
# 科学计数法保留两位小数
>>> '{:.2e}'.format(554.45488)
'5.54e+02'

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

部分文档

format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::= <any character>
align       ::= "<" | ">" | "=" | "^"
sign        ::= "+" | "-" | " "
width       ::= integer
precision   ::= integer
type        ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"

#一些例子
# 对齐和填充(指定宽度对齐才能生效)
   >>> '{:<30}'.format('left aligned')
   'left aligned                  '
   >>> '{:>30}'.format('right aligned')
   '                 right aligned'
   >>> '{:^30}'.format('centered')
   '           centered           '
   >>> '{:*^30}'.format('centered')  # use '*' as a fill char
   '***********centered***********'

# 数值前的正负号显示方式
   >>> '{:+f}; {:+f}'.format(3.14, -3.14)  # show it always
   '+3.140000; -3.140000'
   >>> '{: f}; {: f}'.format(3.14, -3.14)  # show a space for positive numbers
   ' 3.140000; -3.140000'
   >>> '{:-f}; {:-f}'.format(3.14, -3.14)  # show only the minus -- same as '{:f}; {:f}'
   '3.140000; -3.140000'

# 使用不同进制显示并控制进制前缀的输出
   >>> # format also supports binary numbers
   >>> "int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42)
   'int: 42;  hex: 2a;  oct: 52;  bin: 101010'
   >>> # with 0x, 0o, or 0b as prefix:
   >>> "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)
   'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'

# 逗号分割大整数
   >>> '{:,}'.format(1234567890)
   '1,234,567,890'

# 百分数形式输出
   >>> points = 19.5
   >>> total = 22
   >>> 'Correct answers: {:.2%}'.format(points/total)
   'Correct answers: 88.64%'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

完整文档请在python命令行中执行如下命令查看 ( FORMATTING两边的分号是必要的 )

help('FORMATTING'
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/1018844
推荐阅读
相关标签
  

闽ICP备14008679号