格式字符串与连接

Format strings vs concatenation

我看到很多人使用这样的格式字符串:

root = "sample"
output = "output"
path = "{}/{}".format(root, output)

而不是像这样简单地连接字符串:

path = root + '/' + output

格式字符串是否具有更好的性能,还是只是为了美观?

只是为了好看。一看就知道是什么格式。我们中的许多人更喜欢可读性而不是微优化。

让我们看看 IPython 的 %timeit 是怎么说的:

Python 3.7.2 (default, Jan  3 2019, 02:55:40)
IPython 5.8.0
Intel(R) Core(TM) i5-4590T CPU @ 2.00GHz

In [1]: %timeit root = "sample"; output = "output"; path = "{}/{}".format(root, output)
The slowest run took 12.44 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 5: 223 ns per loop

In [2]: %timeit root = "sample"; output = "output"; path = root + '/' + output
The slowest run took 13.82 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 5: 101 ns per loop

In [3]: %timeit root = "sample"; output = "output"; path = "%s/%s" % (root, output)
The slowest run took 27.97 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 5: 155 ns per loop

In [4]: %timeit root = "sample"; output = "output"; path = f"{root}/{output}"
The slowest run took 19.52 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 5: 77.8 ns per loop

与大多数事情一样,性能会有差异,但请问问自己 "Does it really matter if this is ns faster?"。 root + '/' output 方法打出来又快又容易。但是当你有多个变量要打印出来时,这很难真正快速阅读

foo = "X = " + myX + " | Y = " + someY + " Z = " + Z.toString()

foo = "X = {} | Y= {} | Z = {}".format(myX, someY, Z.toString())

哪个更容易理解是怎么回事?除非你真的需要提高性能,否则请选择最容易让人阅读和理解的方式

字符串格式绑定数据时不受数据类型限制。在串联时,我们必须相应地键入转换或转换数据。

例如:

a = 10
b = "foo"
c = str(a) + " " + b
print c
> 10 foo

可以通过字符串格式来完成:

a = 10
b = "foo"
c = "{} {}".format(a, b)
print c
> 10 foo

这样with-in占位符{} {},我们假设有两件事要进一步,即在这种情况下,是ab

为了代码的美观和维护。如果使用格式,编辑代码真的会更容易。此外,当您使用 + 时,您可能会错过空格等细节。为您和可能的维护者使用格式。

它不仅适用于 "looks",也适用于强大的词法类型转换;也是国际化的必经之路。

您可以根据选择的语言换出格式字符串。

随着源代码中包含一长串字符串连接,这实际上变得不可能正确完成。

我同意格式主要用于提高可读性,但自从 3.6 中的 f-strings 发布以来,表格在性能方面发生了变化。我还认为 f 字符串更多 readable/maintainable,因为 1) 它们可以像大多数常规文本一样从左到右阅读,以及 2) 由于变量在字符串中,因此避免了与间距相关的连接缺点.

运行 此代码:

from timeit import timeit

runs = 1000000


def print_results(time, start_string):
    print(f'{start_string}\n'
          f'Total: {time:.4f}s\n'
          f'Avg: {(time/runs)*1000000000:.4f}ns\n')


t1 = timeit('"%s, %s" % (greeting, loc)',
            setup='greeting="hello";loc="world"',
            number=runs)
t2 = timeit('f"{greeting}, {loc}"',
            setup='greeting="hello";loc="world"',
            number=runs)
t3 = timeit('greeting + ", " + loc',
            setup='greeting="hello";loc="world"',
            number=runs)
t4 = timeit('"{}, {}".format(greeting, loc)',
            setup='greeting="hello";loc="world"',
            number=runs)

print_results(t1, '% replacement')
print_results(t2, 'f strings')
print_results(t3, 'concatenation')
print_results(t4, '.format method')

在我的机器上产生这个结果:

% replacement
Total: 0.3044s
Avg: 304.3638ns

f strings
Total: 0.0991s
Avg: 99.0777ns

concatenation
Total: 0.1252s
Avg: 125.2442ns

.format method
Total: 0.3483s
Avg: 348.2690ns

.

上给出了对不同问题的类似答案

从 Python 3.6 开始,您可以通过在字符串前添加 f 来实现 literal string interpolation

foo = "foo"
bar = "bar"
path = f"{foo}/{bar}"