f-string formatting:
This is new in Python 3.6 - the string is enclosed in quotation marks, as usual, with the addition of f'... same as you would r'... for the raw string. Then you put everything you want to put in your string, variables, numbers, inside curly braces f'some string text with a {variable} or {number} within that text' - and Python evaluates as with the previous string formatting methods, for except that this method is much more readable.
>>>a = 3.141592 >>>print(f'My number is {a:.2f} - look at the nice rounding!') My number is 3.14 - look at the nice rounding!
You can see in this example that we format with decimal places similarly to the previous string formatting methods.
NB a can be a number, a variable, or even an expression, for example, f'{3*my_func(3.14):02f}' .
Further, with the new code, f-lines should be preferable to the usual methods% s or str.format (), since f-lines are much faster .
toonarmycaptain Sep 29 '17 at 19:06 on 2017-09-29 19:06
source share