string formatting in Python

Qianjiang Hu
2022-04-16

% Operator

%d will truncate to integer, %s will maintain formatting, %f will print as float and %g is used for generic number %x format specifier convert an int value to a string and represent it as a hexadecimal number

It’s also possible to refer to variable substitutions by name if passing a mapping to the % operator

'{}'.format()

f-Strings

f'{}'

Template Strings (Standard Library)