码迷,mamicode.com
首页 > 其他好文 > 详细

2016.07.09-10 字符串格式化

时间:2016-07-15 20:01:08      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

字符串格式化
    格式化方式有两种:
    printf styleformat

        printf style形式:
            template % tuple 
            template % dict (当单个元素反复出现,或者需要格式化的内容很多的情况下,可以使用字典)
        example:
            >>> i am %s, my name is %s % (ZJ,ZJ)
            i am ZJ, my name is zj
            >>> i am %(name)s, my name is %(name)s % {name: ZJ}
            i am ZJ, my name is ZJ
            >>> 
        格式化字符类型:
            Flag        Meaning 
            #        The value conversion will use the “alternate form” (where defined below). 
            0        The conversion will be zero padded for numeric values. 
            -        The converted value is left adjusted (overrides the 0 conversion if both are given). 
                     (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion. 
            +        A sign character (+ or -) will precede the conversion (overrides a “space” flag). 

            Conversion    Meaning
            d        Signed integer decimal.   
            i        Signed integer decimal.   
            o        Signed octal value.
            u        Obsolete type – it is identical to d.
            x        Signed hexadecimal (lowercase).
            X        Signed hexadecimal (uppercase).
            e        Floating point exponential format (lowercase). 
            E        Floating point exponential format (uppercase).
            f        Floating point decimal format.
            F        Floating point decimal format.
            g        Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.
            G        Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.
            c        Single character (accepts integer or single character string).   
            r        String (converts any Python object using repr()).
            s        String (converts any Python object using str()).
            a        String (converts any Python object using ascii()).
            %        No argument is converted, results in a % character in the result.   

            >>> %d % 18                #整形
            18
            >>> %i % 18                #整形
            18
            >>> %o % 18                #八进制
            22
            >>> %u % 18                #整形
            18
            >>> %x % 12                #十六进制(小写)
            c
            >>> %x % 12                #十六进制(大写)  
            X
            >>> %e % 0.0000123            #用科学计数法表示浮点数(小写)
            1.230000e-05
            >>> %E % 0.0000123            #用科学计数法表示浮点数(大写)
            1.230000E-05
            >>> %f % 0.00000123            #表示浮点数,默认显示小数点后6位
            0.000001
            >>> %F % 0.00000123            #表示浮点数,默认显示小数点后6位
            0.000001
            >>> %g % 0.000000000001234567        #当浮点小数,小数点后的0超过4个时使用科学计数法显示(小写)
            1.23457e-12
            >>> %G % 0.000000000001234567        #当浮点小数,小数点后的0超过4个时使用科学计数法显示(大写)
            1.23457E-12
            >>> %c % c                #单个字符,如果字符是多个会抛出异常。
            c
            >>> %s % python is good language    #字符串(str)
            python is good language
            >>> %r % python is good language    #字符串(repr)
            "‘python is good language‘"
            >>> %a % \n                #字符串(ascii)
            "‘\\n‘"
            >>>


        format方法
        example:
            >>> i am {}.format(ZJ)
            i am ZJ
            >>> i am {}, my age is {}.format(ZJ, 18)
            i am ZJ, my age is 18
            >>> i am {0}, my age is {1}.format(ZJ, 18)
            i am ZJ, my age is 18
            >>> i am {name}, my age is {age}.format(name=ZJ, age=18)    
            i am ZJ, my age is 18
            >>> i am {name}, my name is {name}.format(name=ZJ)                       
            i am ZJ, my name is ZJ
            >>> i am {0}, my age is {0}.format(ZJ)                   
            i am ZJ, my age is ZJ  
            >>> {name} {} {}.format(1,2,name=3)   #传递参数时,位置参数一定要在关键字参数前面。
            3 1 2
            >>> {name} {1} {2}.format(1,2,name=3)
            Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
            IndexError: tuple index out of range
            >>> {name} {1} {2}.format(1,1,2,name=3)
            3 1 2
            >>> {name} {0} {1}.format(1, 2, name=3)  
            3 1 2
            >>> {name} {0} {1}.format(1, 2, name=3)

            >>> l1 = [1, 2, 3]
            >>> {0[2]} % l1
            {0[2]}
            >>> {0[2]}.format(l1)
            3
            >>> 

            >>> {0:^30}.format(ZJ)        #居中
                          ZJ              
            >>> {0:<30}.format(ZJ)        #左对齐
            ZJ                            
            >>> {0:>30}.format(ZJ)
                                        ZJ    #右对齐
            >>> 

            >>> {0:+}.format(10)            
            +10
            >>> {0:+}.format(-10)
            -10

            >>> {0:30}.format(ZJ)        #设置宽度,字符串默认是左对齐
            ZJ                            
            >>> {0:30}.format(30)  
                                        30    #设置宽度,数字默认时右对齐
            >>>
            
            >>> {0:d}.format(10)            #d为指定类型为数字
            10
            >>>

            >>> {0:{fill}^{width}}.format(30,width=30,fill=!)    #参数是可以进行嵌套的,{full}表示对齐时用指定字符进行填充,默认是用空格进行填充。
            !!!!!!!!!!!!!!30!!!!!!!!!!!!!!
            >>> 

 

2016.07.09-10 字符串格式化

标签:

原文地址:http://www.cnblogs.com/LouisZJ/p/5674548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!