• Python学习笔记:字符串前加f、r、b、u


    一、字符串前加"f"

    1. %

    可以使用 % 格式化字符串。

    c = (250, 250)
    # 使用 % 格式化
    s1 = "坐标为:%s" % c
    # TypeError: not all arguments converted during string formatting
    s1 = "坐标为:%s" % (c,) # '坐标为:(250, 250)'
    # 使用 format 格式化
    s2 = "坐标为:{}".format(c) # '坐标为:(250, 250)'
    

    2.format

    Python 2.6 引入 format 格式化字符串的方式。

    str.format() 是对 %-formatting 的改进,替换字段使用大括号 {} 标记。

    "Hello, {}. You are {}.".format(name, age)
    
    • 可以通过引用索引来改变引入顺序:
    age = 100
    name = 'Hider'
    "Hello {1}.You are {0}-{0}.".format(age, name)
    # 'Hello Hider.You are 100-100.'
    
    • 如果插入变量名称,则会获得额外传递对象的权限:
    person = {'name':'Hider', 'age':100}
    "Hello {name}.You are {age}.".format(age=person['age'], name=person['name'])
    # 'Hello Hider.You are 100.'
    
    • 使用 * 可以针对列表进行解码:
    data = ["Hider", 100]
    "Name:{0}, Age:{1}.".format(*data)
    # 'Name:Hider, Age:100.'
    
    • 使用 ** 来使用字典完成巧妙技巧:
    person = {'name':'Hider', 'age':100}
    "Hello {name}.You are {age}.".format(**person) # 解开字典形成独立形参
    # 'Hello Hider.You are 100.'
    
    • 填充与对齐

    ^、<、> 分别是居中、左对齐、右对齐,后面带宽度参数。

    : 后面带填充的字符,只能是一个字符,默认空格填充。

    "{:>10}".format('18') # '        18'
    "{:0>10}".format('18') # '0000000018'
    "{:A>10}".format('18') # 'AAAAAAAA18'
    "{:A^10}".format('18') # 'AAAA18AAAA'
    
    • zfill 方法补充

    zfill方法返回指定长度的字符串,原字符串右对齐,前面填充0,使用语法为:

    str.zfill(width)
    
    "18".zfill(10) # '0000000018'
    
    • 精度与类型f

    精度常跟类型f一起使用:

    "{:.2f}".format(3.1415926) # '3.14'
    # 小数点后2位 float类型
    
    • 其他进制

    b、d、o、x分别代表二进制、十进制、八进制、十六进制。

    "{:b}".format(18) # '10010'
    "{:d}".format(18) # '18'
    "{:o}".format(18) # '22'
    "{:x}".format(18) # '12'
    
    • 千位分隔符
    "{:,}".format(1234567890) # '1,234,567,890'
    

    str.format( )是一个升级版本,代码易读,但当处理多个参数和更长字符串时,非常冗长,每个变量都要指明。

    使用字典的方式 .format(**dict) 解压,并通过字符串中的键值引用。

    3.Python 3.6 之 f'{}' —— 一种改进Python格式字符串的新方法

    格式化字符串常量(formatted string literals)是 Python 3.6 新引入的一种字符串格式化方法,主要目的是使格式化字符串的操作更加简便。

    f-string在形式上是以 f 或者 F 修饰符引领的字符串(f'xxx' 或 F'xxx'),以大括号 {} 标明被替代的字段。

    f-string本质上不是字符串产常量,而是一个在运行时运算求值的表达式。

    # 创建
    my_dict = {'name':'Hider',
               'age':'100'}
    
    print(f"My name is {my_dict['name']}, I'm {my_dict['age']}.")
    # My name is Hider, I'm 100.
    
    import time
    t0 = time.time()
    time.sleep(1)
    name = 'processing'
    # f支持在字符串内使用大括号{}的python表达式
    print(f"{name} done in {time.time() - t0:.2f} s.")
    # processing done in 1.00 s.
    

    二、字符串前加"r"

    r 的作用是去除转义字符。

    例如: 表示反斜杠字符 + 字母n,而不是换行符。

    r 开头的字符,常用于正则表达式,对应 re 模块。

    str1 = 'input
    '
    str2 = r'input
    '
    print(str1) # input 并换行
    print(str2) # input
    
    

    三、字符串前加"b"

    b 的作用是表示一个 bytes 对象。

    网络编程中,服务器和浏览器只认 bytes 类型数据。

    例如:

    response = b'<h1>Hello World!</h1>'
    

    send 函数的参数和 recv 函数返回值都是 bytes 类型。

    在 Python3 中,bytes 和 str 相互转换方式:

    str.encode('utf-8') # 编码
    bytes.decode('utf-8') # 解码
    

    四、字符串前加"u"

    u 的作用是表示字符串以 Unicode 格式进行编码。

    一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时出现乱码。

    例如:

    u'我是中文字符串。'
    

    参考链接1:python 字符串前面加 f的含义

    参考链接2:python中 r'', b'', u'', f'' 的含义

    参考链接3:python3.6---之f'{}'

    参考链接4:python的格式化输出(format,%)

  • 相关阅读:
    MySQL临时表
    git开发常用命令
    PHP资源列表
    Golang学习--平滑重启
    Golang学习--TOML配置处理
    Golang学习--包管理工具glide
    Golang学习--开篇
    构建自己的PHP框架--构建模版引擎(3)
    构建自己的PHP框架--构建模版引擎(2)
    Laravel Session 遇到的坑
  • 原文地址:https://www.cnblogs.com/hider/p/14705476.html
Copyright © 2020-2023  润新知