Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/data/messages/c/consider-using-f-string/bad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from string import Template
menu = ('eggs', 'spam', 42.4)

old_order = "%s and %s: %.2f ¤" % menu # [consider-using-f-string]
beginner_order = menu[0] + " and " + menu[1] + ": " + str(menu[2]) + " ¤"
joined_order = " and ".join(menu[:2])
format_order = "{} and {}: {:0.2f} ¤".format(menu[0], menu[1], menu[2]) # [consider-using-f-string]
named_format_order = "{eggs} and {spam}: {price:0.2f} ¤".format(eggs=menu[0], spam=menu[1], price=menu[2]) # [consider-using-f-string]
template_order = Template('$eggs and $spam: $price ¤').substitute(eggs=menu[0], spam=menu[1], price=menu[2])
7 changes: 7 additions & 0 deletions doc/data/messages/c/consider-using-f-string/details.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Formatted string literals (f-strings) give a concise, consistent syntax
that can replace most use cases for the ``%`` formatting operator,
``str.format()`` and ``string.Template``.

F-strings can also perform better than alternatives; see
`this tweet <https://twitter.com/raymondh/status/1205969258800275456>`_ for
a simple example.
3 changes: 3 additions & 0 deletions doc/data/messages/c/consider-using-f-string/good.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
menu = ('eggs', 'spam', 42.4)

f_string_order = f"{menu[0]} and {menu[1]}: {menu[2]:0.2f} ¤"