3

Python F-Strings 比你想象的更强大

 1 year ago
source link: https://www.51cto.com/article/746159.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
95bcdac6657eb81598a94828ab0c01ac3c66c0.png

格式化字符串文字——也称为f 字符串——自 Python 3.6 以来就已经存在,所以我们都知道它们是什么以及如何使用它们。然而,你可能不知道 f-strings的一些比较实用跟方便的功能。因此让这篇文章带你了解一下f-strings的一些功能,希望你在日常编码中使用的这些很棒的 f-strings功能。

日期和时间格式

使用 f 字符串应用数字格式非常常见,但你知道你还可以格式化日期和时间戳字符串吗?

import datetime
today = datetime.datetime.today()
print(f"{today:%Y-%m-%d}")
# 2023-02-03
print(f"{today:%Y}")
# 2023

f-strings 可以像使用datetime.strftime方法一样格式化日期和时间。当你意识到除了文档中提到的几种格式之外还有更多格式时,这非常好。Python strftime还支持底层 C 实现支持的所有格式,这可能因平台而异,这就是文档中未提及的原因。话虽如此,你仍然可以利用这些格式并使用例如%F,它等效于%Y-%m-%d或%T等效于%H:%M:%S,还值得一提的是%x和%X分别是语言环境首选的日期和时间格式。这些格式的使用显然不限于 f 字符串。有关时间格式的完整列表请参阅:
https://manpages.debian.org/bullseye/manpages-dev/strftime.3.en.html

变量名和调试

f-string 功能(从 Python 3.8 开始)最近新增的功能之一是能够打印变量名称和值:

x = 10
y = 25
print(f"x = {x}, y = {y}")
# x = 10, y = 25
print(f"{x = }, {y = }")  # Better! (3.8+)
# x = 10, y = 25

print(f"{x = :.3f}")
# x = 10.000

此功能称为“调试”,可以与其他修饰符结合使用。它还保留空格,因此f"{x = }"和f"{x=}"将产生不同的字符串。

字符串表示

打印类实例时,__str__默认使用类的方法来表示字符串。但是,如果我们想强制使用__repr__,我们可以使用!r转换标志:

class User:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    def __str__(self):
        return f"{self.first_name} {self.last_name}"

    def __repr__(self):
        return f"User's name is: {self.first_name} {self.last_name}"

user = User("John", "Doe")
print(f"{user}")
# John Doe
print(f"{user!r}")
# User's name is: John Doe

我们也可以只repr(some_var)在 f 字符串内部调用,但使用转换标志是一个很好的习惯和简洁的解决方案。

f-strings杰出的性能表现

强大的功能和语法糖通常会带来性能损失,但对于 f 字符串而言情况并非如此:

# python -m timeit -s 'x, y = "Hello", "World"' 'f"{x} {y}"'
from string import Template

x, y = "Hello", "World"

print(f"{x} {y}")  # 39.6 nsec per loop - Fast!
print(x + " " + y)  # 43.5 nsec per loop
print(" ".join((x, y)))  # 58.1 nsec per loop
print("%s %s" % (x, y))  # 103 nsec per loop
print("{} {}".format(x, y))  # 141 nsec per loop
print(Template("$x $y").substitute(x=x, y=y))  # 1.24 usec per loop - Slow!

上面的示例使用timeit如下模块进行了测试:python -m timeit -s 'x, y = "Hello", "World"' 'f"{x} {y}"'正如你所看到的,f 字符串实际上是 Python 提供的所有格式化选项中最快的。因此,即使你更喜欢使用一些较旧的格式化选项,你也可以考虑切换到 f-strings 只是为了提高性能。

格式化规范的全部功能

F-strings 支持 Python 的Format Specification Mini-Language,所以你可以在它们的修饰符中嵌入很多格式化操作:

text = "hello world"

# Center text:
print(f"{text:^15}")
# '  hello world  '

number = 1234567890
# Set separator
print(f"{number:,}")
# 1,234,567,890

number = 123
# Add leading zeros
print(f"{number:08}")
# 00000123

Python 的Format Specification Mini-Language不仅仅包括格式化数字和日期的选项。它允许我们对齐或居中文本、添加前导零/空格、设置千位分隔符等等。所有这些显然不仅适用于 f 字符串,而且适用于所有其他格式设置选项。

嵌套 f-strings

如果基本的 f-strings 不足以满足你的格式化需求,你甚至可以将它们相互嵌套:

number = 254.3463
print(f"{f'${number:.3f}':>10s}")
# '  $254.346'

你可以将 f-strings 嵌入 f-strings 中以解决棘手的格式化问题,例如将美元符号添加到右对齐的浮点数,如上所示。

如果你需要在格式说明符部分使用变量,也可以使用嵌套的 f 字符串。这也可以使 f 字符串更具可读性:

import decimal
width = 8
precision = 3
value = decimal.Decimal("42.12345")
print(f"output: {value:{width}.{precision}}")
# 'output:     42.1'

在上面带有嵌套 f 字符串的示例之上,我们可以更进一步,在内部 f 字符串中使用三元条件运算符:

import decimal
value = decimal.Decimal("42.12345")
print(f'Result: {value:{"4.3" if value < 100 else "8.3"}}')
# Result: 42.1
value = decimal.Decimal("142.12345")
print(f'Result: {value:{"4.2" if value < 100 else "8.3"}}')
# Result:      142

lambda表达式

如果你想突破 f-strings 的限制,同时让阅读你代码的人觉得你很牛逼,那么你可以使用 lambdas

print(f"{(lambda x: x**2)(3)}")
# 9

在这种情况下,lambda 表达式周围的括号是强制性的,因为:否则将由 f 字符串解释。

正如我们在这里看到的,f-strings确实非常强大,并且具有比大多数人想象的更多的功能。然而,大多数这些"未知"特性在 Python 文档中都有提及,因此我建议你不仅阅读 f-strings,还阅读你可能使用的任何其他 Python 模块/特性的文档页面。深入研究文档通常会帮助你发现一些非常有用的功能。


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK