Python 格式化字符串与连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38722105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:20:58  来源:igfitidea点击:

Format strings vs concatenation

pythonstring-formatting

提问by wjk2a1

I see many people using format strings like this:

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

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

Instead of simply concatenating strings like this:

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

path = root + '/' + output

Do format strings have better performance or is this just for looks?

格式字符串有更好的性能还是只是为了外观?

采纳答案by kay - SE is evil

It's just for the looks. You can see at one glance what the format is. Many of us like readability better than micro-optimization.

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

Let's see what IPython's %timeitsays:

让我们看看 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

回答by Eric Ed Lohmar

I agree that the formatting is mostly used for readability, but since the release of f-strings in 3.6, the tables have turned in terms of performance. It is also my opinion that the f-strings are more readable/maintainable since 1) they can be read left-right like most regular text and 2) the spacing-related disadvantages of concatenation are avoided since the variables are in-string.

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

Running this code:

运行此代码:

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')

yields this result on my machine:

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

% 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

A similar answer to a different question is given on this answer.

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

回答by Lightness Races in Orbit

It's not just for "looks", or for powerful lexical type conversions; it's also a must for internationalisation.

这不仅仅是为了“外观”,或者是为了强大的词法类型转换;这也是国际化的必要条件。

You can swap out the format string depending on what language is selected.

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

With a long line of string concatenations baked into the source code, this becomes effectively impossible to do properly.

由于源代码中包含一长串字符串连接,因此实际上无法正确执行此操作。

回答by FuriousGeorge

As with most things, there will be a performance difference, but ask yourself "Does it really matter if this is ns faster?". The root + '/' outputmethod is quick and easy to type out. But this can get hard to read real quick when you have multiple variables to print out

与大多数事情一样,会有性能差异,但问问自己“如果这快 ns 真的重要吗?”。该root + '/' output方法快速且易于输入。但是当您有多个变量要打印时,这可能很难快速阅读

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

vs

对比

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

Which is easier to understand what is going on? Unless you reallyneed to eak out performance, chose the way that will be easiest for people to read and understand

哪个更容易理解发生了什么?除非你真的需要提高性能,否则选择最容易让人们阅读和理解的方式

回答by Muhammad Yaseen Khan

String format is free of data type while binding data. While in concatenation we have to type cast or convert the data accordingly.

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

For example:

例如:

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

It could be done via string formatting as:

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

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

Such that with-in placeholders {} {}, we assume two things to come further i.e., in this case, are aand b.

这样,内占位符{} {},我们假设两件事更进一步,即在这种情况下,是ab

回答by Cyzanfar

As of Python 3.6 you can do literal string interpolationby prepending fto the string:

从 Python 3.6 开始,您可以通过添加到字符串来进行文字字符串插值f

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

回答by Doruk

It's for looks and the maintaining of the code. It's really easier to edit your code if you used format. Also when you use + you may miss the details like spaces. Use format for your and possible maintainers' good.

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