f'approx pi: {math.pi:.3f}' # 'approx pi: 3.142'
f'approx pi: {math.pi:.10f}' # 'approx pi: 3.1415926536'
# padding...
foo = 'bar'
baz = 'qux'
f'{foo:10}' # 'bar '
f'{foo:10} => {baz}' # 'bar => qux'
'{}'.format('foo') # 'foo'
'{1}'.format('foo', 'bar') # 'bar'
'{foo} {baz}'.format(foo='bar', baz='qux') # 'bar qux'
'{0[foo]} {0[baz]}'.format({'foo': 'bar', 'baz': 'qux'}) # 'bar qux'
'{0[foo]:f} {0[baz]:f}'.format({'foo': 1.23, 'baz': 4.5}) # '1.230000 4.500000'
Note: in last example, if you would use the type to trigger an exception if the input didn't match:
'{0[foo]:f} {0[baz]:d}'.format({'foo': 1.23, 'baz': 4.5})
would raise aValueError: Unknown format code 'd' for object of type 'float'
.
'foo%s' % 'bar' # foobar
'foo%(bar)s' % {'foo': 'FOO', 'bar': 'BAR'} # fooBAR
'num: %f' % 1.2 # 'num: 1.200000'
'pi: %5.3f' % math.pi # 'pi: 3.142'