小白教程 发表于 2021-4-4 14:19:01

小白教程:python中format用法

format是python2.6新增的一个格式化字符串的方法,相对于老版的%格式方法,它有很多优点。1.不需要理会数据类型的问题,在%方法中%s只能替代字符串类型2.单个参数可以多次输出,参数顺序可以不相同3.填充方式十分灵活,对齐方式十分强大4.官方推荐用的方式,%方式将会在后面的版本被淘汰format的一个例子 print
         
         'hello {0}'
         .
         format
         (
         'world'
         )会输出hello worldformat的格式replacement_field   ::=“{” [“!” conversion] [“:” format_spec] “}”
field_name            ::=      arg_name (“.” attribute_name | “[” element_index “]”)*
arg_name               ::=      
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   ::=  [align][#][,][.precision]
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.通过位置来填充字符串print
         
         'hello {0} i am {1}'
         .
         format
         (
         'Kevin'
         ,
         'Tom'
         )
                           
         # hello Kevin i am Tom
      

      

         print
         
         'hello {} i am {}'
         .
         format
         (
         'Kevin'
         ,
         'Tom'
         )
                           
         # hello Kevin i am Tom
      

      

         print
         
         'hello {0} i am {1} . my name is {0}'
         .
         format
         (
         'Kevin'
         ,
         'Tom'
         )
         
         # hello Kevin i am Tom . my name is Kevinforamt会把参数按位置顺序来填充到字符串中,第一个参数是0,然后1 ……也可以不输入数字,这样也会按顺序来填充同一个参数可以填充多次,这个是format比%先进的地方2.通过key来填充print
         
         'hello {name1}i am {name2}'
         .
         format
         (
         name1
         =
         'Kevin'
         ,
         name2
         =
         'Tom'
         )
                           
         # hello Kevin i am Tom3.通过下标填充names
         =
         [
         'Kevin'
         ,
         'Tom'
         ]
      

      

         print
         
         'hello {names}i am {names}'
         .
         format
         (
         names
         =
         names
         )
                           
         # hello Kevin i am Tom
      

      

         print
         
         'hello {0}i am {0}'
         .
         format
         (
         names
         )
                                       
         # hello Kevin i am Tom
      4.通过字典的key names
         =
         {
         'name'
         :
         'Kevin'
         ,
         'name2'
         :
         'Tom'
         }
      

      

         print
         
         'hello {names}i am {names}'
         .
         format
         (
         names
         =
         names
         )
                           
         # hello Kevin i am Tom注意访问字典的key,不用引号的5.通过对象的属性class
         
         Names
         (
         )
         :
      

      

            
         name1
         =
         'Kevin'
      

      

            
         name2
         =
         'Tom'
      

      

         
      

      

         print
         
         'hello {names.name1}i am {names.name2}'
         .
         format
         (
         names
         =
         Names
         )
                           
         # hello Kevin i am Tom6.使用魔法参数args
         =
         [
         'lu'
         ]
      

      

         kwargs
         
         =
         
         {
         'name1'
         :
         
         'Kevin'
         ,
         
         'name2'
         :
         
         'Tom'
         }
      

      

         print
         
         'hello {name1} {} i am {name2}'
         .
         format
         (
         *
         args
         ,
         
         *
         *
         kwargs
         )
         
         # hello Kevin i am Tom

页: [1]
查看完整版本: 小白教程:python中format用法