Skip to content

Commit 8d608c9

Browse files
Add documentation examples of consider-using-f-string (#6460)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent d77adce commit 8d608c9

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from string import Template
2+
3+
menu = ('eggs', 'spam', 42.4)
4+
5+
old_order = "%s and %s: %.2f ¤" % menu # [consider-using-f-string]
6+
beginner_order = menu[0] + " and " + menu[1] + ": " + str(menu[2]) + " ¤"
7+
joined_order = " and ".join(menu[:2])
8+
format_order = "{} and {}: {:0.2f} ¤".format(menu[0], menu[1], menu[2]) # [consider-using-f-string]
9+
named_format_order = "{eggs} and {spam}: {price:0.2f} ¤".format(eggs=menu[0], spam=menu[1], price=menu[2]) # [consider-using-f-string]
10+
template_order = Template('$eggs and $spam: $price ¤').substitute(eggs=menu[0], spam=menu[1], price=menu[2])
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Formatted string literals (f-strings) give a concise, consistent syntax
2+
that can replace most use cases for the ``%`` formatting operator,
3+
``str.format()`` and ``string.Template``.
4+
5+
F-strings also perform better than alternatives; see
6+
`this tweet <https://twitter.com/raymondh/status/1205969258800275456>`_ for
7+
a simple example.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
menu = ('eggs', 'spam', 42.4)
2+
3+
f_string_order = f"{menu[0]} and {menu[1]}: {menu[2]:0.2f} ¤"

0 commit comments

Comments
 (0)