Programming Python. and Testing Snippets.
Wonder how this and that should/could go in Python?
$ python -m doctest format_string_examples.txt
This command tests a textfile that tries some snippets you probably need, somehow, somewhere and have some more interest to understand what's going on.
format_string_examples.txt
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
>>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only
'a, b, c'
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'
>>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence
'c, b, a'
>>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated
'abracadabra'
>>> "{:20}{}".format("Betreff", "Hello Zurich")
'Betreff Hello Zurich'
To round x to n decimal places use:
>>> import math
>>> n = 4
>>> p = math.pi
>>> '{0:.{1}f}'.format(p, n)
'3.1416'
$ python -m doctest format_string_examples.txt
or with some more info
$ python -m doctest format_string_examples.txt -v