zzwwsun

IT小白的白话江湖

Python format()函数以及字符串格式化

format函数:
作用:
格式化输出字符串

        格式:
            ''.format(value[,format_spec=' '])

        示例:
            #位置匹配
            >>> sun = 'sunhziwen' 
            >>> s =43
            >>> 'my name is {}.'.format(sun)
            'my name is sunhziwen.'
            >>> 'my name is {1},I\'m {0} years old.'.format(s,sun)
            "my name is sunhziwen,I'm 43 years old."

            #关键字或字典
            >>> s = {'name':'sunzhiwen', 'age':25}
            >>> 'my name is {name},I\'m {age} years old.'.format(**s)  #**字典
            "my name is sunzhiwen,I'm 25 years old."

            >>> 'my name is {name},I\'m {age} years old.'.format(name='sunzhiwen',age=23)
            "my name is sunzhiwen,I'm 23 years old."

            #对齐:
            >>> print('{:^14}'.format("sunzhiwen"))
              sunzhiwen   
            >>> print('{:>14}'.format("sunzhiwen"))
                 sunzhiwen
            >>> print('{:*>14}'.format("sunzhiwen"))
            *****sunzhiwen
            >>> print('{:<14}'.format("sunzhiwen"))
            sunzhiwen     
            >>> print('{:*<14}'.format("sunzhiwen"))
            sunzhiwen*****

            #精度:
            >>> print('{:.4f}'.format(3.14))
            3.1400
            >>> print('{:.4f}'.format(3.141592656469))
            3.1416

            #进制转换
            >>> print('{:b}'.format(250))
            11111010
            >>> print('{:o}'.format(250))
            372
            >>> print('{:d}'.format(250))
            250
            >>> print('{:x}'.format(250))
            fa

            #千分位分隔符:
            >>> print('{:,}'.format(100000000000000))
            100,000,000,000,000
            >>> print('{:,}'.format(123456489.21335498))
            123,456,489.21335497

字符串格式化表达式
作用:
生成一定格式的字符串

    运算符:
        %

    格式:
        格式化字符串%参数值
        或
        格式化字符串%(参数值1,参数值2,...)

    示例:
        fmt = '姓名:%s, 年龄:%d'
        name = '张飞'
        age = 20
        print(fmt % (name, age))

格式化字符串中的占位符和类型码

    占位符类型码                  意义
        %s                  字符串
        %r                  字符串,使用repr,而不是str

        %c                  整数转为单个字符
        >>> "%c" %20013     #返回值:'中',类似于chr(20013)

        %d                  十进制整数
            >>> "%d" %10        #返回值:'10'
        %o                  八进制整数
            >>> "%o" %10        #返回值:'12'
        %x                  十六进制整数(字符a-F小写)
            >>> "%x" %10        #返回值:'a'
        %X                  十六进制整数(字符A-F大写)
            >>> "%X" %10        #返回值:'A'

        %e                  指数型浮点数(e小写),如2.9e+10
            >>> "%e" %29979000000.0    #返回值:'2.997900e+10'
        %E                  指数型浮点数(E大写),如2.9E+10
            >>> "%E" %29979000000.0    #返回值:'2.997900E+10'

        %f,                 %F  浮点十进制形式
            >>> "%f" %29979000000.0   #返回值:'29979000000.000000'
            >>> "%F" %29979000000.0   #返回值:'29979000000.000000'
            >>> "%F" %2.997e+10       #返回值:'29970000000.000000'

        %g,                 %G  十进制形式浮点或指数浮点自动转换
            >>> "%g" %12.123                        #返回值:'12.123'
            >>> "%g" %12545361564531.123321564      #返回值:'1.25454e+13'

        %%                  等同于一个%字符
            >>> "利率为%f%%" %1.8                    #返回值:'利率为1.800000%'

    占位符%和类型码之间的格式语法:

        % [格式语法] 类型码 (a,s,f等)

            格式语法:
                -       左对齐
                +       右对齐
                0       补零
                宽度(十进制数字)
                宽度.精度(十进制数字)                #f,F

            示例:

                >>> "%10d" %123                   #返回值:'       123'
                >>> "%-10d" %123                  #返回值:'123       '

                >>> "%10s" %123                   #返回值:'       123'
                >>> "%10s" %'abc'                 #返回值:'       abc'
                >>> "%-5s" %'abc'                 #返回值:'abc  '

                >>> "%+10d" %123                  #返回值:      +123'
                >>> "%010d" %123                  #返回值:'0000000123'
                >>> "%0010d" %123                 #返回值:'0000000123'

                >>> "%020.5f" %123.123545689      #返回值:'00000000000123.12355'
                >>> "%0-20.5f" %123.123545689     #返回值:'123.12355           '
                >>> "%-020.5f" %123.123545689     #返回值:'123.12355           '
                >>> "%+020.5f" %123.123545689     #返回值:'+0000000000123.12355'

                >>> "%4.4f" %3.1415926            #返回值:'3.1416'
                >>> "%2.4f" %3.1415926            #返回值:'3.1416'
阅读更多
文章标签: python format
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭