zoukankan      html  css  js  c++  java
  • Python:字符串格式化

    Python中提供了多种格式化字符串的方式,遇到一个项目,在一个文件中,就用了至少两种方式。特别是在使用Log时,更让人迷惑。

    因此特地花时间来了解一下Python中字符串格式化的几种方式:

    # -*- coding: utf-8 -*-
    
    import unittest
    class StringFormatTests(unittest.TestCase):
        def test_C_style(self):
            """
                % 是老风格的字符串格式化方式, 也称为C语言风格,语法如下:
                    %[(name)][flags][width].[precision]type
                name: 是占位符名称
                flags: 可取值有:
                    - 左对齐(默认右对齐)
                    0 表示使用0填充(默认填充是空格)
                  表示显示宽度
                precision:表示精度,即小数点后面的位数
                type:
                    %s string,采用str()显示
                    %r string,采用repr()显示
                    %c char
                    %b 二进制整数
                    %d, %i 十进制整数
                    %o 八进制整数
                    %x 十六进制整数
                    %e 指数
                    %E 指数
                    %f, %F 浮点数
                    %% 代表字符‘%’
    
                注意,如果内容中包括%,需要使用%%来替换
            :return:
            """
            # 测试 flag 与 width
            print("%-10x" % 20)
            print("%-10x" % 20)
            print("%10x" % 20)
            print("%010x" % 20)
    
            # 测试 name的用法:
            print("hello, %s, my name is %s, age is %d" % ("lucy", "zhangsan", 20))
            print("hello, %(yname)s, my name is %(myname)s, age is %(myage)d" % {"yname":"lucy", "myname":"zhangsan", "myage":20})
    
        def test_new_style_format(self):
            """
            string.format() 提供了新风格的格式化, 语法格式:{:format_spec}
            format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
                fill        ::=  <any character>
                align       ::=  "<" | ">" | "=" | "^" 对齐方式,分别是左、右、两边、中间对齐
                sign        ::=  "+" | "-" | " "
                width       ::=  integer
                precision   ::=  integer
                type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"  ,默认值是 "s"
    
            详情参见:https://docs.python.org/2/library/string.html
    
            因为{}要作为格式化的标志位,所以内容尽量不要使用'{'或'}'
            另外,可以使用索引
    
            相比而言,新风格的比老风格的强大太多了。
    
            :return:
            """
            print('hello, {0}, {2}, {1}'.format('a', 'b', 'c'))
    
        def test_template(self):
            """这种风格的,可以看作是类似于Linux变量风格的
                ${var}
                $var
                $$ 代表单个字符 '$'
            """
            from string import Template
            s = Template('$who likes $what')
            print(s.substitute(who='tim', what='kung pao'))
            pass
    
    if __name__ =="main":
        unittest.main()
  • 相关阅读:
    如何将php数组或者对象传递给javascript
    js函数定时器,定时读取系统实时连接数
    采用highchart js+flot+rrd生成cpu、mem状态监控图
    jquery动态加载 去除js
    js控制href内容的连接内容的变化
    js动态控制table的tr,td增加及删除
    php生成PDF文件(FPDF)
    .NET 单点登录开源项目
    WF控制台工作流(2)
    WF控制台工作流(1)
  • 原文地址:https://www.cnblogs.com/f1194361820/p/9674775.html
Copyright © 2011-2022 走看看