内容简介:在 Python 中,提供了很多种字符串格式化的方式,分别是这种格式化方式来自于 C 语言风格的 sprintf 形式:C 语言的给实话风格深入人心,通过
起步
在 Python 中,提供了很多种字符串格式化的方式,分别是 %-formatting
、 str.format
和 f-string
。本文将比较这几种格式化方法。
%- 格式化
这种格式化方式来自于 C 语言风格的 sprintf 形式:
name = "weapon" "Hello, %s." % name
C 语言的给实话风格深入人心,通过 %
进行占位。
为什么 %-formatting不好
不好的地方在于,如果字符串较长或较多的参数,那么可读性就变得很差。
str.format 格式化
PEP-3101
带来了 str.format
,它是对 %-formatting
的改进。它使用正常的函数调用语法,并且可以通过对要转换为字符串的对象的 __format __()
方法进行扩展。
"Hello, {}. You are {}.".format(name, age)
并支持字典形式传参,免于位置参数带来的麻烦:
"Hello, {name}. You are {age}.".format(name=name, age=age)
这两种方式代码效果相同,只是第一种方法需要严格控制传入的参数位置,而第二种方法没有这种限制, 并增加了代码的可读性。各种技巧可查看 Format Specification Mini-Language
为什么 str.format() 并不好
虽然它解决了字符串冗长情况下的可读性,但需要对字典传参基本是要重写一遍变量名,不够优雅。
f-string 格式化
PEP-0498
带来了 f-string
方式,它从 Python3.6 开始支持。这种方式也是使用 __format__
协议进行格式化。
name = "Eric"
age = 74
f"Hello, {name}. You are {age}."
语法上与 str.format()
类似,但更为简洁,当字符串较长时也不会繁琐。更强大的是它支持任意的表达式。我们可以在花括号内进行四则运算或函数调用等: f"{2 * 6}"
或者 f"{name.lower()} is funny"
。
并且它性能也最好。
几种格式化方式性能比较
import timeit
def add():
status = 200
body = 'hello world'
return 'Status: ' + str(status) + '\r\n' + body + '\r\n'
def old_style():
status = 200
body = 'hello world'
return 'Status: %s\r\n%s\r\n' % (status, body)
def formatter1():
status = 200
body = 'hello world'
return 'Status: {}\r\n{}\r\n'.format(status, body)
def formatter2():
status = 200
body = 'hello world'
return 'Status: {status}\r\n{body}\r\n'.format(status=status, body=body)
def f_string():
status = 200
body = 'hello world'
return f'Status: {status}\r\n{body}\r\n'
perf_dict = {
'add': min(timeit.repeat(lambda: add())),
'old_style': min(timeit.repeat(lambda: old_style())),
'formatter1': min(timeit.repeat(lambda: formatter1())),
'formatter2': min(timeit.repeat(lambda: formatter2())),
'f_string': min(timeit.repeat(lambda: f_string())),
}
print(perf_dict)
结果:
{
'add': 0.8815229000000002,
'old_style': 0.6351808999999999,
'formatter1': 0.7536176999999995,
'formatter2': 1.2277180999999997,
'f_string': 0.4891379000000011
}
f-string
格式化的方式性能最好。
为何 f-string 速度如此快
从指令来看, f'Status: {status}\r\n{body}\r\n'
翻译成:
8 LOAD_CONST 3 ('Status: ')
10 LOAD_FAST 0 (status)
12 FORMAT_VALUE 0
14 LOAD_CONST 4 ('\r\n')
16 LOAD_FAST 1 (body)
18 FORMAT_VALUE 0
20 LOAD_CONST 4 ('\r\n')
22 BUILD_STRING 5
正如指令中所示的,f-string 是运行时渲染的,底层中转成了类似 "Status: " + status+ "\r\n" + body + "\r\n"
的形式。正如 PEP-0498
中提到的:
F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with 'f', which contains expressions inside braces. The expressions are replaced with their values.
而其他方式则是要先创建字符串常量值,再进行替换之类的操作。
总结
我们仍然可以使用以前的方式进行格式化,但在此推荐 f-string
方式,因为它使用更简洁,更易读且更方便,性能又更好,完全没理由拒绝啊。
从今天开始使用 f-string
!
以上所述就是小编给大家介绍的《Python 中几种字符串格式化方法及其比较》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
数据结构与算法:Python语言描述
裘宗燕 / 机械工业出版社 / 2016-1 / CNY 45.00
本书基于Python语言介绍了数据结构与算法的基本知识,主要内容包括抽象数据类型和Python面向对象程序设计、线性表、字符串、栈和队列、二叉树和树、集合、排序以及算法的基本知识。本书延续问题求解的思路,从解决问题的目标来组织教学内容,注重理论与实践的并用。一起来看看 《数据结构与算法:Python语言描述》 这本书的介绍吧!
随机密码生成器
多种字符组合密码
HTML 编码/解码
HTML 编码/解码