and you can fill in the name of the person greeted, and combine given text with a chosen insertion. We use this as an analogy: Python has a similar construction, called a formatted string or an f-string. An f-string makes substitutions into places in a string enclosed in braces. Run this code:
The string has been formatted in a new way. We have included an f before the starting quotation mark. Such a string is called an f-string. Places where braces are embedded are replaced by the value of the expression inside the braces. There are many variations on the syntax between the braces. In this case we use the syntax where the first (and only) location in the string with braces has the variable person. When this code is evaluated, the value of the person variable is placed in the string in this location.
The identifier greeting was introduced to break the operations into a clearer sequence of steps. However, since the value of greeting is only referenced once, it can be eliminated with the more concise version:
If you used the data suggested, this result is not satisfying. Prices should appear with exactly two places beyond the decimal point, but that is not the default way to display floats.
F-strings can give further information inside the braces showing how to specially format data. In particular, floats can be shown with a specific number of decimal places. For two decimal places, put :.2f inside the braces but after the variable name for the monetary values:
A technical point: Since braces have special meaning in a format string, there must be a special rule if you want braces to actually be included in the final formatted string. The rule is to double the braces: { { and }}. For example mathematical set notation uses braces. The initial and final doubled braces in the format string below generate literal braces in the formatted string: