• 【python基础】format格式化函数的使用



    前言

    语法:str.format()
    说明:一种格式化字符串的函数。


    一、format()内容匹配替换

    1、序号索引

    在没有参数序号时,参数是按顺序使用的。

    主要格式:print(‘{} {}’.format(‘str1’,‘str2’))

    print('{} {}'.format('hello','world'))
    ##输出结果:hello world
    
    • 1
    • 2

    可以通过format()参数序号指定参数的使用,参数从0开始编号(与索引编号规律一致)

    主要格式:print(‘{0} {1}’.format(‘str1’,‘str2’))

    print('{0} {1}'.format('hello','world'))
    输出结果:hello world
    
    • 1
    • 2

    一个参数可以多次插入

    print('{0} {1} {0}'.format('hello','world'))
    ##输出结果:hello world hello
    
    print('{1} {1} {0}'.format('hello','world'))
    ##输出结果:world world hello
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、关键字

    主要格式:print(‘{关键字索引1} {关键字索引2}’.format(关键字索引1=‘关键字1’,关键字索引2=‘关键字2’))

    obj = 'world'
    name = 'lily'
    print('hello {obj},my name is {name}'.format(obj=obj,name = name))
    
    ##输出结果:hello world,my name is lily
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3、列表索引

    主要格式:

    • list1=[‘列表值1’,‘列表值2’,‘列表值3’…]
    • print(‘{list[列表索引1],list[列表索引2]}’.format(list=list1))

    在format格式化时,可以使用*或者**对list进行拆分。

    • print(‘{list[列表索引1],list[列表索引2]}’.format(*list1))
    list = ['world','lily']
    print('hello {names[0]},my name is {names[1]}'.format(names=list))
    ##输出结果:hello world,my name is lily
    
    list = ['world','lily']
    print('hello {0[0]},my name is {0[1]}'.format(list)) #"0"是必须的。
    ##输出结果:hello world,my name is lily
    
    list = ['world','lily']
    print('hello {0},my name is {1}'.format(*list)) #"0"是必须的。
    ##输出结果:hello world,my name is lily
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4、字典索引

    主要格式:

    • dict1={‘键1’:‘值1’,‘键2’:‘值2’,…}
    • print(‘{dict[键1]},{dict[键2]}’.format(dict=dict1))

    在format格式化时,可以使用**进行字典拆分。

    • print(‘{dict1[键1]},{dict1[键2]}’.format(**dict1))
    dict={"obj":"world","name":"lily"}
    print('hello {names[obj]},my name is {names[name]}'.format(names=dict))
    ##输出结果:hello world,my name is lily
    
    dict={"obj":"world","name":"lily"}
    print('hello {obj},my name is {name}'.format(**dict))
    ##输出结果:hello world,my name is lily
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5、通过类的属性

    主要格式:

    • 类定义
      class 类名字():
      类内定义=‘类值’

      print(‘value.类内定义’.format(value=类名字))
    class Value1():
    	obj='lily'
    	name='world'
    print('hello {value.obj} , my name is {value.name}'.format(value=Value1))
    
    ##输出结果:hello world,my name is lily
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    6、通过魔法参数

    args=[',','one']
    kwargs={"obj":"world","name":"lily"}
    print('hello {obj},my name is {name}'.format(*args,**kwargs))
    ##输出结果:hello world,my name is lily
    
    print('hello {obj},my name is {name}'.format(',','one',obj='world',name='lily'))
    ##输出结果:hello world,my name is lily
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    魔法参数跟函数中使用的性质是一样的,魔法参数详情可参考文档:魔法参数*args, **kwargs的使用

    二、format()数字格式化

    :<填充><对齐><符号><宽度>,<精度><类别>
    用于填充单个字符^ 表示居中,
    <表示左对齐,
    > 表示右对齐
    + 表示在正数前显示 +,负数前显示 -;
    - 表示正数前不显示,负数前显示;
    空格表示正数前加空格,负数前加负号;
    #对于二进制数、八进制数和十六进制数,使用此参数,各进制数前会分别显示 0b、0o、0x前缀;
    反之则不显示前缀。
    槽的设定,
    输出宽度
    数字的千位分隔符,适用于整数和浮点数浮点数小数部分的精度或字符串的最大输出长度整数类型b,d,o,x ,
    浮数类型e,E,f,%

    b,d,o,x 分别是二进制,十进制,八进制,十六进制。
    e,E,f,% 分别是小写字母e的科学计数法,大写字母E的科学计数法,转换为浮点数(默认小数点后六位),显示百分比(默认小数点后六位)

    举例:

    • <填充>与<宽度>
    print('{:0>2d}'.format(5)) #数字补零,填充左边,宽度为2
    ##输出结果:05
    
    print('{:x<4d}'.format(5)) #数字补x,填充左边,宽度为4
    ##输出结果:5xxx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • <对齐>与<宽度>
    print('{:>10d}'.format(13))  #右对齐 (默认, 宽度为10)
    ##输出结果:        13
    
    print('{:<10d}'.format(13))  #左对齐,宽度为10
    ##输出结果:13        
    
    print('{:^10d}'.format(13))  #中间对齐,宽度为10
    ##输出结果:    13    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • <,>
    print('{:,}'.format(1000000)) #以逗号分隔数字格式。
    ##输出结果:1,000,000
    
    • 1
    • 2
    • <.精度> (注意里面有个点号)
    print('{:.5}'.format(12.397539)) #保留5位数
    ##输出结果:12.398
    
    • 1
    • 2
    • <符号>与 <类型>
    print('{:.2%}'.format(0.25)) #百分比格式
    ##输出结果:25.00%
    
    print('{:.2e}'.format(100000000)) #指数记法
    ##输出结果:1.00e+08
    
    print('{:.2f}'.format(3.1415926)) #保留小数点后两位
    ##输出结果:3.14
    
    print('{:+.2f}'.format(3.1415926)) #带符号保留小数点后两位
    ##输出结果:+3.14
    
    print('{:-.2f}'.format(-1)) #带符号保留小数点后两位
    ##输出结果:-1.00
    
    print('{:.0f}'.format(2.89)) #不带小数,四舍五入
    ##输出结果:3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    print('{:b}'.format(11)) #二进制表示 
    ##输出结果:1011
    
    print('{:d}'.format(11)) #十进制表示
    ##输出结果:11
    
    print('{:o}'.format(11)) #八进制表示
    ##输出结果:13
    
    print('{:x}'.format(11)) #十六进制表示
    ##输出结果:b
    
    print('{:#X}'.format(11))
    ##输出结果:0XB
    
    print('{:#x}'.format(11))
    ##输出结果:0xb
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    参考文档:
    https://blog.csdn.net/weixin_69553582/article/details/129830874

  • 相关阅读:
    Linux安装Oracle和postgrepSQL数据库
    Vue或uniapp引入自定义字体
    Java--Lambda(1)简介
    【排坑】websoucket场景下文件无法上传到服务器的解决方案
    史上最全ubuntu18.04安装教程|搜狗输入法配置教程|网络配置|相关命令配置
    IDEA插件开发(1)--- 插件的主要类型
    SQL 左连接 LEFT JOIN 关键字||SQL右连接 RIGHT JOIN 关键字
    JAVA动态代理
    【MACOS(M1)编译Risc-v版OpenOCD】
    【牛客网】OR63 删除公共字符串
  • 原文地址:https://blog.csdn.net/sodaloveer/article/details/134133286