string module python code example
Example 1: modulo str python
print("there is %d ducks in the lake" % 34)
your_string = "%d of the %d ducks are ducklings"
values = (12, 32)
print(your_string % values)
Formaters are:
%s to display as string (and do str(value) if needed)
%i to display in default int form
%d to display as a decimal integer
%x to display as an hexadecimal intedger (with lettre in lowercase)
%X to display as an hexadecimal intedger (with lettre in uppercase)
%o to display as an octal integer
%u to display as an unsigned integer
%c to display in ascii (process "chr(value)")
%f to display in default float form
%e to display in scientific writing (with a lowercase e to expess exponent)
%E to display in scientific writing (with an uppercase E to expess exponent)
%g to display as an floating exponent
Example 2: string template python
from string import Template
poem = Template('$x are red and $y are blue')
print(poem.substitute(x='roses', y='violets'))