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

    将值转换为字符串并设置格式是拼接字符串输出的重要手段。更灵活方便。join方法拼接只能使用分隔符,且要求被拼接的必须是可迭代对象。由于Python是强类型语言,+拼接字符串要求非字符串必须先转换成字符串

    C语言风格的格式化字符串

    Python2.5版本之前,拼接字符串的解决方案主要使用字符串格式设置运算符——百分号。类似于C语言中函数printf。

    使用格式:

    1. 占位符:使用%和格式字符组成,如%s、%d等。s调用str()将不是字符串的对象转换为字符串。r调用repr()。
    2. 修饰符:占位符中可以使用修饰符,例如:%.3f表示保留3位小数,%03d表示打印3个位置,位数不够用0补全
    3. format % values,格式字符串和被格式的值之间用%分隔
    4. values可以是一个和格式字符串占位符数目相等的元组,或者字典
    5. 默认右对齐,使用 - 号左对齐

    使用示例1:

    format="hello %s. %s a day!"
    
    values=("world","What")
    
    format % values

    运行结果:

    'hello world. What a day!'

    使用示例2:

    import math
    format="π is %.5f"
    values=(math.pi,)
    format % values

    运行结果:

    'π is 3.14159'

    使用示例3:

    "%010.3f%% 0x%x,0X%02X" % (99.99999,10,15)

    运行结果

    '000100.000% 0xa,0X0F'

    使用示例4:

    "the num is %-4d" %(20,)

    运行结果

    'the num is 20  '

    Python目前版本的格式化字符串

    format方法格式化字符串语法

    "{} {xxx}".format(*args,**kwargs) -> str

    其中:

    1. args 是位置参数,是元组
    2. kwargs 是关键字参数,是字典
    3. 花括号表示占位符,按照顺序匹配元组中每个位置参数,{n} 表示取位置参数索引为n的值
    4. {xxx} 表示在关键字参数中搜索名称一致的
    5. {{}} 表示打印花括号

    位置参数用法

    In [85]: "{} {} {}".format("first","second","third")
    Out[85]: 'first second third'

    关键字参数

    In [86]: "{name}'s  {age}".format(age=18,name="tom")
    Out[86]: "tom's  18"

    位置参数和关键字参数的混合使用

    In [89]: "{foo} {} {bar} {}".format(1,2,foo=3,bar=4)
    Out[89]: '3 1 4 2'

    可迭代对象元素访问

    In [87]: '{0[0]} {0[1]} or not {0[0]} {0[1]} !'.format(('to','be'))
    Out[87]: 'to be or not to be !'

    对象属性访问

    In [88]: from collections import namedtuple
        ...: people=namedtuple('_p',['name','age'])
        ...: p=people(age=18,name="tom")
        ...: "{{{0.name}'s {0.age}}}".format(p)
    Out[
    88]: "{tom's 18}"

    格式化打印中的对齐(默认右对齐)

    In [90]: "{} * {} = {}".format(3,2,3*2)
    Out[90]: '3 * 2 = 6'
    
    In [91]: "{} * {} = {:>02}".format(3,2,3*2)
    Out[91]: '3 * 2 = 06'
    
    In [92]: "{} * {} = {:02}".format(3,2,3*2)
    Out[92]: '3 * 2 = 06'
    
    In [93]: "{} * {} = {:<02}".format(3,2,3*2)
    Out[93]: '3 * 2 = 60'

    居中

    In [95]: "{:^10}".format('哈哈')
    Out[95]: '    哈哈    '
    
    In [96]: "{:=^10}".format('哈哈')
    Out[96]: '====哈哈===='

    进制

    In [97]: "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42)
    Out[97]: 'int: 42; hex: 2a; oct: 52; bin: 101010'
    
    In [98]: "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)
    Out[98]: 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'
    
    In [99]: 'the num is {num:f}'.format(num=10)
    Out[99]: 'the num is 10.000000'
    
    In [100]: 'the num is {num:b}'.format(num=10)
    Out[100]: 'the num is 1010'

    基本转换总结

    字符串格式中的类型说明符

    类型 含义
    b 整数表示为二进制数
    c 将整数解读为Unicode码点
    d 将整数视为十进制数,这是整数默认使用的说明符
    e 使用科学计数法表示小数(e表示指数)
    E 与e相同,但是使用E表示指数
    f 将小数表示为定点数
    F 与f相同,但对于特殊值(nan和inf)使用大写表示
    g 自动在定点表示法和科学计数法中选择,默认用于小数的说明符,默认情况下至少有1位小数
    G 与g相同,但使用大写表示指数和特殊值
    n 与g相同,但插入随区域而异的数字分隔符
    o 将整数表示为八进制数
    s 保持字符串的格式,默认用于字符串的说明符
    x 将整数表示为十六进制数并使用小写字母
    X 与x相同,但使用大写字母
    % 将数表示为百分比值
  • 相关阅读:
    SharePoint 2007中的Permission Level与Permission之间的关系
    如何确定一个SharePoint列表的ID?
    经典线程死锁
    SharePoint的Workflow History列表在哪里?
    SharePoint Designer中设计的workflow出错了, 怎么办?
    清除MOSS的Timer Job Cache的方法 Clear the SharePoint Configuration Cache
    sp_MSforeachtable和sp_MSforeachdb
    sa安全
    几个排名函数的区别
    递归限制
  • 原文地址:https://www.cnblogs.com/zh-dream/p/13422705.html
Copyright © 2011-2022 走看看