File tree Expand file tree Collapse file tree 3 files changed +20
-0
lines changed
doc/data/messages/c/consider-using-f-string Expand file tree Collapse file tree 3 files changed +20
-0
lines changed Original file line number Diff line number Diff line change 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 ])
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 1+ menu = ('eggs' , 'spam' , 42.4 )
2+
3+ f_string_order = f"{ menu [0 ]} and { menu [1 ]} : { menu [2 ]:0.2f} ¤"
You can’t perform that action at this time.
0 commit comments