(Jan-02-2022, 01:06 AM)barryjo Wrote: 123456.345 and I want to see 123456.3
Then is
n:.1f
>>> n = 123456.345
>>> print(f"The number with one decimal place {n:.1f}")
The number with one decimal place 123456.3
>>> print(f"The number with ten decimal place {n:.10f}")
The number with 10 decimal place 123456.3450000000
f'{value:{width}.{precision}}'
width
specifies the number of characters used in total to display.
Some more stuff with f-string
>>> n = 123456
>>> print(f"The number in binary {n:02b}")
The number in binary 11110001001000000
>>> print(f"The number in hex {n:02X}")
The number in hex 1E240
# f-strings can take any Python expressions inside the curly braces
>>> grapes = 5.20
>>> apples = 9.70
>>> print(f'The price is {apples + grapes:.2f}$')
The price is 14.90$
# Also methods eg .upper()
>>> for word in 'f-strings are cool'.split():
... print(f'{word.upper():~^20}')
...
~~~~~F-STRINGS~~~~~~
~~~~~~~~ARE~~~~~~~~~
~~~~~~~~COOL~~~~~~~~