Analysis and comparison of several string formatting methods in Python

  • 2021-07-06 11:30:45
  • OfStack

Start

In Python, there are many ways to format strings, which are%-formatting, str. format and f-string. This article will compare these formatting methods.

%-Formatting

This formatting comes from the sprintf form of C language style:


name = "weapon"
"Hello, %s." % name

C language to the truth style deeply rooted in the hearts of the people, through% to occupy space.

Why%-formatting is not good

The downside is that if the string is long or has more parameters, the readability becomes poor.

str. format Formatting

PEP-3101 brings str. format, which is an improvement on%-formatting. It uses normal function call syntax and can be extended by the __format __ () method on the object to be converted to a string.


"Hello, {}. You are {}.".format(name, age)

It also supports dictionary form parameter transmission to avoid the trouble caused by position parameters:


"Hello, {name}. You are {age}.".format(name=name, age=age)

The two methods have the same code effect, except that the first method needs to strictly control the position of passed parameters, while the second method does not have this restriction and increases the readability of the code. For various tips, see Format Specification Mini-Language

Why is str. format () not good

Although it solves the readability in the case of lengthy strings, it needs to rewrite the variable name once for dictionary reference, which is not elegant enough.

f-string Formatting

PEP-0498 brings the f-string mode, which is supported from Python 3.6. This approach is also formatted using the __format__ protocol.


name = "Eric"
age = 74
f"Hello, {name}. You are {age}."

Syntax is similar to str. format (), but it is more concise and not cumbersome when the string is long. What is more powerful is that it supports arbitrary expressions. We can do 4 operations or function calls in curly braces: f"{2 * 6}" Or f"{name.lower()} is funny" .

And it has the best performance.

Performance comparison of several formatting methods


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)

Results:


{
  'add': 0.8815229000000002, 
  'old_style': 0.6351808999999999, 
  'formatter1': 0.7536176999999995, 
  'formatter2': 1.2277180999999997, 
  'f_string': 0.4891379000000011
}

The f-string format performs best.

Why is f-string so fast

According to the instructions, f'Status: {status}\r\n{body}\r\n' Translated into:


 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

As shown in the instruction, f-string is rendered at run time, and the underlying layer is converted to a form similar to "Status:" + status + "\ r\ n" + body + "\ r\ n". As mentioned in 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.

Other methods are to create string constant values first, and then replace them.

Summarize

We can still use the previous formatting method, but we recommend f-string because it is simpler, easier to read and more convenient to use, and has better performance, so there is no reason to refuse it.

Use f-string from today!

Above is this site to introduce several string formatting methods in Python and its comparison, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!


Related articles: