all that is technology

Python f-string


f-strings is a short for "formatted strings", provides a convenient way to embed expressions inside string literals. This supports more formatting options than the old str.format() method supports.

Few examples;

String operation

>>> name = 'Arunkumar'
>>> f'My name is {name.upper()}'
'My name is ARUNKUMAR'

Float formatting

>>> import math
>>> f'math.pi:.5f'

Datetime formatting

this_day =
f'Yesteday was {this_day + datetime.timedelta(days=-1):%A} and tommorow is {this_day + datetime.timedelta(days=-1):%A}'
'Yesterday was Wednesday and tommorow is Friday'

f-string token looks like;

f ' <text> { <expression> <optional !s, !r, or !a> <optional : format specifier> } <text> ... '

Starting from 3.8 f-string supports "=" symbol for documenting expressions

>>> sum = 5 + 6
>>> f'{sum=}'

Hope this helps!