zoukankan      html  css  js  c++  java
  • 第四天 PYTHON 字符串格式化

    字符串格式化的笔记就摘抄一下他人的吧,做一个记录,以后有需要的时候再回来查询。

    感谢笔记提供者,虽然也没跟人家打招呼,呵呵。

    原贴地址如下:

    https://www.cnblogs.com/nulige/p/6115793.html

    ##################################################################################################

    Python基础-字符串格式化_百分号方式_format方式

     

    Python的字符串格式化有两种方式: 百分号方式、format方式

    百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存。[PEP-3101]

    This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing '%' string formatting operator.

    1、百分号方式

    • (name)      可选,用于选择指定的key
    • flags          可选,可供选择的值有:width         可选,占有宽度
      • +       右对齐;正数前加正好,负数前加负号;
      • -        左对齐;正数前无符号,负数前加负号;
      • 空格    右对齐;正数前加空格,负数前加负号;
      • 0        右对齐;正数前无符号,负数前加负号;用0填充空白处
    • .precision   可选,小数点后保留的位数
    • typecode    必选
      • s,获取传入对象的__str__方法的返回值,并将其格式化到指定位置
      • r,获取传入对象的__repr__方法的返回值,并将其格式化到指定位置
      • c,整数:将数字转换成其unicode对应的值,10进制范围为 0 <= i <= 1114111(py27则只支持0-255);字符:将字符添加到指定位置
      • o,将整数转换成 八  进制表示,并将其格式化到指定位置
      • x,将整数转换成十六进制表示,并将其格式化到指定位置
      • d,将整数、浮点数转换成 十 进制表示,并将其格式化到指定位置
      • e,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(小写e)
      • E,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(大写E)
      • f, 将整数、浮点数转换成浮点数表示,并将其格式化到指定位置(默认保留小数点后6位)
      • F,同上
      • g,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是e;)
      • G,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是E;)
      • %,当字符串中存在格式化标志时,需要用 %%表示一个百分号

    注:Python中百分号格式化是不存在自动将整数转换成二进制表示的方式

    常用格式化:

    复制代码
     1 tpl = "i am %s" % "alex"
     2  
     3 tpl = "i am %s age %d" % ("alex", 18)
     4  
     5 tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
     6  
     7 tpl = "percent %.2f" % 99.97623    #打印浮点数
     8  
     9 tpl = "i am %(pp).2f" % {"pp": 123.425556, }
    10  
    11 tpl = "i am %.2f %%" % {"pp": 123.425556, }    #打开百分比
    复制代码

    示例:

    1、%s 可以字符串拼接

    1 msg='i am %s my hobby is %s' % ('lhf','alex')
    2 print(msg)

    执行结果:

    1 i am lhf my hobby is alex

    2、%s 可以按收任何类型( 数字对应字符串)

    1 msg='i am %s my hobby is %s' % ('lhf',1)
    2 print(msg)

    执行结果:

    1 i am lhf my hobby is 1

    3、%s 可以字符串接收列表

    1 msg='i am %s my hobby is %s' % ('lhf',[1,2])
    2 print(msg)

    执行结果:

    1 i am lhf my hobby is [1, 2]

    4、%s传字符串或任何值, %d只能接收数字

    1 name='lhf'
    2 age=19
    3 msg='i am %s my hobby is %d' % (name,age)  #age可以用%d or %s,但用%s程序可读性差
    4 print(msg)

    执行结果:

    1 i am lhf my hobby is 19

    5、%d 只能接收数字类型,不能接收列表

    1 # %d 只能按收数字类型
    2 msg='i am %s my hobby is %d' % ('lhf',1)
    3 print(msg)

    执行结果:

    1 i am lhf my hobby is 1

    6、%d 不能接收列表类型,会报错

    1 msg='i am %s my hobby is %d' % ('lhf',[1,2])
    2 print(msg)

    执行结果:

    1 Traceback (most recent call last):
    2   File "D:/python/day5/format_type.py", line 14, in <module>
    3     msg='i am %s my hobby is %d' % ('lhf',[1,2])
    4 TypeError: %d format: a number is required, not list

    7、打印浮点数

    1 tpl = "percent %.2f" % 99.976234444444444444
    2 print(tpl)

    执行结果:

    1 percent 99.98

    8、打印百分比

    1 tpl = 'percent %.2f %%' % 99.976234444444444444
    2 print(tpl)

    执行结果:

    1 percent 99.98 %

    9、左边打印空格

    1 msg='i am %(name)+60s my hobby is alex' %{'name':'lhf'}
    2 print(msg)

    执行结果:

    1 i am                                                          lhf my hobby is alex

    10、打印空格加颜色(黄色)

    1 msg='i am 33[43;1m%(name)+60s33[0m my hobby is alex' %{'name':'lhf'}
    2 print(msg)

    执行结果:

    1 i am                                                          lhf my hobby is alex

    19、不用格式化的方式

    1 print('root','x','0','0',sep=':')
    2 print('root'+':'+'x'+':'+'0','0')

    执行结果:

    1 root:x:0:0
    2 root:x:0 0

    2、Format 方式

    [[fill]align][sign][#][0][width][,][.precision][type]
    • fill           【可选】空白处填充的字符
    • align        【可选】对齐方式(需配合width使用)
      • <,内容左对齐
      • >,内容右对齐(默认)
      • =,内容右对齐,将符号放置在填充字符的左侧,且只对数字类型有效。 即使:符号+填充物+数字
      • ^,内容居中
    • sign         【可选】有无符号数字
      • +,正号加正,负号加负;
      •  -,正号不变,负号加负;
      • 空格 ,正号空格,负号加负;
    • #            【可选】对于二进制、八进制、十六进制,如果加上#,会显示 0b/0o/0x,否则不显示
    • ,            【可选】为数字添加分隔符,如:1,000,000
    • width       【可选】格式化位所占宽度
    • .precision 【可选】小数位保留精度
    • type         【可选】格式化类型
      • 传入” 字符串类型 “的参数
        • s,格式化字符串类型数据
        • 空白,未指定类型,则默认是None,同s
      • 传入“ 整数类型 ”的参数
        • b,将10进制整数自动转换成2进制表示然后格式化
        • c,将10进制整数自动转换为其对应的unicode字符
        • d,十进制整数
        • o,将10进制整数自动转换成8进制表示然后格式化;
        • x,将10进制整数自动转换成16进制表示然后格式化(小写x)
        • X,将10进制整数自动转换成16进制表示然后格式化(大写X)
      • 传入“ 浮点型或小数类型 ”的参数
        • e, 转换为科学计数法(小写e)表示,然后格式化;
        • E, 转换为科学计数法(大写E)表示,然后格式化;
        • f , 转换为浮点型(默认小数点后保留6位)表示,然后格式化;
        • F, 转换为浮点型(默认小数点后保留6位)表示,然后格式化;
        • g, 自动在e和f中切换
        • G, 自动在E和F中切换
        • %,显示百分比(默认显示小数点后6位)

     常用格式化:

    
    
    复制代码
     1 tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
     2   
     3 #必须一一对应,否则会报错
     4 tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
     5   
     6 
     7 tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
     8   
     9 
    10 tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
    11   
    12 
    13 tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
    14   
    15 
    16 tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
    17                                                      ** 代表传字典
    18 
    19 tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
    20   
    21 
    22 tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
    23              s 代表字符串 d 代表整数
    24 
    25 tpl = "i am {:s}, age {:d}".format(*["seven", 18])
    26                                    * 代表列表
    27 
    28 tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
    29   
    30 
    31 tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
    32  
    33 
    34 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
    35                 2进制 8进制 10进制  x与X: 16进制 %:百分比
    36 
    37 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
    38  
    39 
    40 tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
    41  
    42 
    43 tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
    复制代码


    示例:

    ps1:

    1 tpl = "i am {0}, age {1}, really {2}".format("seven", '18','alex')
    2 print(tpl)

    执行结果:

    1 i am seven, age 18, really alex

    ps2:

    1 tpl = "i am {2}, age {1}, really {0}".format("seven", '18','alex')
    2 print(tpl)

    执行结果:

    1 i am alex, age 18, really seven

    ps3: 传字典的方式

    1 tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
    2 print(tpl)

    执行结果:

    1 i am seven, age 18, really seven

    ps4: ** 传字典的方式 (跟ps3的方式是一样的)

    1 tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
    2 print(tpl)

    执行结果:

    1 i am seven, age 18, really seven

    ps5:  * 代表:列表

    1 tpl = "i am {:s}, age {:d}".format(*["seven", 18])
    2 print(tpl)

    执行结果:

    1 i am seven, age 18

    ps6:

    1 tpl = "i am {:s}, age {:d}".format("seven", 18) #["seven", 18]
    2 print(tpl)

    执行结果:

    1 i am seven, age 18

    ps7:

    1 l=["seven", 18]
    2 tpl = "i am {:s}, age {:d}".format('seven',18)
    3 print(tpl)

    执行结果:

    1 i am seven, age 18

    ps8:   2进制 8进制 10进制 x与X: 16进制 %:百分比

    1 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2)
    2 print(tpl)

    执行结果:

    1 numbers: 1111,17,15,f,F, 1587.623000%,2
  • 相关阅读:
    正则表达式解决身份证号码和手机号
    redis:集群配置
    linux:NFS
    xshell提示必须安装最新的更新
    linux:ssh远程调用tomcat脚本时候出错
    linux:scp从入门到刚入门
    linux:SSH最简单教程
    nginx;keepalived配置出现主主的解决方法(脑裂问题)
    (4)事件处理——(4)网页上的多个脚本(Multiple scripts on one page)
    [php]应用控制器(一)
  • 原文地址:https://www.cnblogs.com/trunkslisa/p/9191395.html
Copyright © 2011-2022 走看看