小白教程

 找回密码
 立即注册
查看: 5387|回复: 0

小白教程:python中format用法

[复制链接]

176

主题

185

帖子

663

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
663
发表于 2021-4-4 14:19:01 | 显示全部楼层 |阅读模式

format是python2.6新增的一个格式化字符串的方法,相对于老版的%格式方法,它有很多优点。

1.不需要理会数据类型的问题,在%方法中%s只能替代字符串类型

2.单个参数可以多次输出,参数顺序可以不相同

3.填充方式十分灵活,对齐方式十分强大

4.官方推荐用的方式,%方式将会在后面的版本被淘汰

format的一个例子

  1. print
  2.          
  3.          'hello {0}'
  4.          .
  5.          format
  6.          (
  7.          'world'
  8.          )
复制代码

会输出hello world

format的格式

replacement_field     ::=  “{” [field_name] [“!” conversion] [“:” format_spec] “}”
field_name              ::=      arg_name (“.” attribute_name | “[” element_index “]”)*
arg_name               ::=      [identifier | integer]
attribute_name       ::=      identifier
element_index        ::=      integer | index_string
index_string           ::=      <any source character except “]”> +
conversion              ::=      “r” | “s” | “a”
format_spec            ::=      <described in the next section>

format_spec 的格式


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” | “%”


应用:

一 填充

1.通过位置来填充字符串

  1. print
  2.          
  3.          'hello {0} i am {1}'
  4.          .
  5.          format
  6.          (
  7.          'Kevin'
  8.          ,
  9.          'Tom'
  10.          )
  11.                            
  12.          # hello Kevin i am Tom
  13.         

  14.         

  15.          print
  16.          
  17.          'hello {} i am {}'
  18.          .
  19.          format
  20.          (
  21.          'Kevin'
  22.          ,
  23.          'Tom'
  24.          )
  25.                              
  26.          # hello Kevin i am Tom
  27.         

  28.         

  29.          print
  30.          
  31.          'hello {0} i am {1} . my name is {0}'
  32.          .
  33.          format
  34.          (
  35.          'Kevin'
  36.          ,
  37.          'Tom'
  38.          )
  39.          
  40.          # hello Kevin i am Tom . my name is Kevin
复制代码

foramt会把参数按位置顺序来填充到字符串中,第一个参数是0,然后1 ……

也可以不输入数字,这样也会按顺序来填充

同一个参数可以填充多次,这个是format比%先进的地方

2.通过key来填充

  1. print
  2.          
  3.          'hello {name1}  i am {name2}'
  4.          .
  5.          format
  6.          (
  7.          name1
  8.          =
  9.          'Kevin'
  10.          ,
  11.          name2
  12.          =
  13.          'Tom'
  14.          )
  15.                            
  16.          # hello Kevin i am Tom
复制代码

3.通过下标填充

  1. names
  2.          =
  3.          [
  4.          'Kevin'
  5.          ,
  6.          'Tom'
  7.          ]
  8.         

  9.         

  10.          print
  11.          
  12.          'hello {names[0]}  i am {names[1]}'
  13.          .
  14.          format
  15.          (
  16.          names
  17.          =
  18.          names
  19.          )
  20.                            
  21.          # hello Kevin i am Tom
  22.         

  23.         

  24.          print
  25.          
  26.          'hello {0[0]}  i am {0[1]}'
  27.          .
  28.          format
  29.          (
  30.          names
  31.          )
  32.                                          
  33.          # hello Kevin i am Tom
  34.         
复制代码

4.通过字典的key

  1. names
  2.          =
  3.          {
  4.          'name'
  5.          :
  6.          'Kevin'
  7.          ,
  8.          'name2'
  9.          :
  10.          'Tom'
  11.          }
  12.         

  13.         

  14.          print
  15.          
  16.          'hello {names[name]}  i am {names[name2]}'
  17.          .
  18.          format
  19.          (
  20.          names
  21.          =
  22.          names
  23.          )
  24.                            
  25.          # hello Kevin i am Tom
复制代码

注意访问字典的key,不用引号的

5.通过对象的属性

  1. class
  2.          
  3.          Names
  4.          (
  5.          )
  6.          :
  7.         

  8.         

  9.             
  10.          name1
  11.          =
  12.          'Kevin'
  13.         

  14.         

  15.             
  16.          name2
  17.          =
  18.          'Tom'
  19.         

  20.         

  21.            
  22.         

  23.         

  24.          print
  25.          
  26.          'hello {names.name1}  i am {names.name2}'
  27.          .
  28.          format
  29.          (
  30.          names
  31.          =
  32.          Names
  33.          )
  34.                            
  35.          # hello Kevin i am Tom
复制代码

6.使用魔法参数

  1. args
  2.          =
  3.          [
  4.          'lu'
  5.          ]
  6.         

  7.         

  8.          kwargs
  9.          
  10.          =
  11.          
  12.          {
  13.          'name1'
  14.          :
  15.          
  16.          'Kevin'
  17.          ,
  18.          
  19.          'name2'
  20.          :
  21.          
  22.          'Tom'
  23.          }
  24.         

  25.         

  26.          print
  27.          
  28.          'hello {name1} {} i am {name2}'
  29.          .
  30.          format
  31.          (
  32.          *
  33.          args
  34.          ,
  35.          
  36.          *
  37.          *
  38.          kwargs
  39.          )
  40.            
  41.          # hello Kevin i am Tom
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 15:07 , Processed in 0.029974 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

快速回复 返回顶部 返回列表