diff --git a/doc/data/messages/c/consider-using-f-string/bad.py b/doc/data/messages/c/consider-using-f-string/bad.py new file mode 100644 index 0000000000..d706d08e2c --- /dev/null +++ b/doc/data/messages/c/consider-using-f-string/bad.py @@ -0,0 +1,10 @@ +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]) diff --git a/doc/data/messages/c/consider-using-f-string/details.rst b/doc/data/messages/c/consider-using-f-string/details.rst new file mode 100644 index 0000000000..6b2d0e48ba --- /dev/null +++ b/doc/data/messages/c/consider-using-f-string/details.rst @@ -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 also perform better than alternatives; see +`this tweet `_ for +a simple example. diff --git a/doc/data/messages/c/consider-using-f-string/good.py b/doc/data/messages/c/consider-using-f-string/good.py new file mode 100644 index 0000000000..eec5abf0fe --- /dev/null +++ b/doc/data/messages/c/consider-using-f-string/good.py @@ -0,0 +1,3 @@ +menu = ('eggs', 'spam', 42.4) + +f_string_order = f"{menu[0]} and {menu[1]}: {menu[2]:0.2f} ¤"