fstring
fstring or f-string ? ¯\_(ツ)_/¯
fstrings are available since python 3.6.
fstrings are used to format a string (before using/printing it).
fstrings are evaluated.
To create a fstring simply put the f letter before your string (your double-quotes "" if you prefer).
A fstring can contain a variable, an expression or a function.
We can also use the : operator to specify the formatting of the expression in the {}.
The formatting is linked to the type of the variable.
For example, :.2f means that we want this float variable printed with only 2 decimals.
:%Y-%m-%d will format a date to year-month-day
Finally if we want to use literal curly braces {} we need to double them.
Be careful when using single-quote or double-quotes to not use the same as the f-string. You can always use triple-quotes to avoid problems if you want.
Remember that a string in python automatically converts backslash caracter (for example \n will be a new line). If you want to keep the raw string, add a r after the f of the fstring.
Oh, and f-strings are faster than .format and % ! 😉
Example
try this example :
Resources
https://youtu.be/BxUxX1Ku1EQ - Python f-strings can do more than you thought. f'{val=}', f'{val!r}', f'{dt:%Y-%m-%d}' - mCoding
https://python.sdv.univ-paris-diderot.fr/03_affichage/#322-prise-en-main-des-f-strings
https://realpython.com/python-f-strings/#multiline-f-strings
https://youtu.be/Mfmr_Puhtew - F-strings In Python: Everything You Need To Know - ArjanCodes