Python 3.6 中的格式化字符串文字是什么?
What are formatted string literals in Python 3.6?
Python 3.6 的功能之一是格式化字符串。
(String with 'f' prefix in python-3.6) 询问格式化字符串文字的内部结构,但我不明白格式化字符串的确切用例字符串文字。我应该在哪些情况下使用此功能?显式不是比隐式好吗?
Simple is better than complex.
所以这里我们格式化了字符串。它使字符串格式化变得简单,同时保持代码明确(与其他字符串格式化机制相比)。
title = 'Mr.'
name = 'Tom'
count = 3
# This is explicit but complex
print('Hello {title} {name}! You have {count} messages.'.format(title=title, name=name, count=count))
# This is simple but implicit
print('Hello %s %s! You have %d messages.' % (title, name, count))
# This is both explicit and simple. PERFECT!
print(f'Hello {title} {name}! You have {count} messages.')
它旨在取代 str.format
以进行简单的字符串格式化。
Pro: F-literal 有更好的性能。(见下文)
缺点:F-literal 是 3.6 的新功能。
In [1]: title = 'Mr.'
...: name = 'Tom'
...: count = 3
...:
...:
In [2]: %timeit 'Hello {title} {name}! You have {count} messages.'.format(title=title, name=name, count=count)
330 ns ± 1.08 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [3]: %timeit 'Hello %s %s! You have %d messages.'%(title, name, count)
417 ns ± 1.76 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [4]: %timeit f'Hello {title} {name}! You have {count} messages.'
13 ns ± 0.0163 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
比较所有 4 种字符串格式化方式的性能
title = 'Mr.'
name = 'Tom'
count = 3
%timeit 'Hello {title} {name}! You have {count} messages.'.format(title=title, name=name, count=count)
每个循环 198 ns ± 7.88 ns(7 次运行的平均值 ± 标准偏差,每次 1000000 次循环)
%timeit 'Hello {} {}! You have {} messages.'.format(title, name, count)
每个循环 329 ns ± 7.04 ns(7 次运行的平均值 ± 标准偏差,每次 1000000 次循环)
%timeit 'Hello %s %s! You have %d messages.'%(title, name, count)
每个循环 264 ns ± 6.95 ns(7 次运行的平均值 ± 标准偏差,每次 1000000 次循环)
%timeit f'Hello {title} {name}! You have {count} messages.'
每个循环 12.1 ns ± 0.0936 ns(7 次运行的平均值 ± 标准差,每次 100000000 次循环)
所以最新的字符串格式化方式来自python3。6也是最快的。
Python 3.6 的功能之一是格式化字符串。
Simple is better than complex.
所以这里我们格式化了字符串。它使字符串格式化变得简单,同时保持代码明确(与其他字符串格式化机制相比)。
title = 'Mr.'
name = 'Tom'
count = 3
# This is explicit but complex
print('Hello {title} {name}! You have {count} messages.'.format(title=title, name=name, count=count))
# This is simple but implicit
print('Hello %s %s! You have %d messages.' % (title, name, count))
# This is both explicit and simple. PERFECT!
print(f'Hello {title} {name}! You have {count} messages.')
它旨在取代 str.format
以进行简单的字符串格式化。
Pro: F-literal 有更好的性能。(见下文)
缺点:F-literal 是 3.6 的新功能。
In [1]: title = 'Mr.'
...: name = 'Tom'
...: count = 3
...:
...:
In [2]: %timeit 'Hello {title} {name}! You have {count} messages.'.format(title=title, name=name, count=count)
330 ns ± 1.08 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [3]: %timeit 'Hello %s %s! You have %d messages.'%(title, name, count)
417 ns ± 1.76 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [4]: %timeit f'Hello {title} {name}! You have {count} messages.'
13 ns ± 0.0163 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
比较所有 4 种字符串格式化方式的性能
title = 'Mr.'
name = 'Tom'
count = 3
%timeit 'Hello {title} {name}! You have {count} messages.'.format(title=title, name=name, count=count)
每个循环 198 ns ± 7.88 ns(7 次运行的平均值 ± 标准偏差,每次 1000000 次循环)
%timeit 'Hello {} {}! You have {} messages.'.format(title, name, count)
每个循环 329 ns ± 7.04 ns(7 次运行的平均值 ± 标准偏差,每次 1000000 次循环)
%timeit 'Hello %s %s! You have %d messages.'%(title, name, count)
每个循环 264 ns ± 6.95 ns(7 次运行的平均值 ± 标准偏差,每次 1000000 次循环)
%timeit f'Hello {title} {name}! You have {count} messages.'
每个循环 12.1 ns ± 0.0936 ns(7 次运行的平均值 ± 标准差,每次 100000000 次循环)
所以最新的字符串格式化方式来自python3。6也是最快的。