In [1]:
from __future__ import print_function

In [2]:
'hello %s world' % 'adjective'


Out[2]:
'hello adjective world'

In [3]:
'hello %s world' % ('foo', 'bar')


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-23557fa28f28> in <module>()
----> 1 'hello %s world' % ('foo', 'bar')

TypeError: not all arguments converted during string formatting

In [4]:
'hello %s %s world' % ('foo', 'bar', 'baz')


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-184cc9e84acc> in <module>()
----> 1 'hello %s %s world' % ('foo', 'bar', 'baz')

TypeError: not all arguments converted during string formatting

In [5]:
'%(language)s has %(number)03d quote types.' % {
    "language": "Python", "number": 2}


Out[5]:
'Python has 002 quote types.'

In [6]:
format = (
    '%(language)s has %(number)03d %(language)019s '
    '%(number)d quote types.')
format % {"language": "Python", "number": 2}


Out[6]:
'Python has 002              Python 2 quote types.'

In [7]:
d = {"language": "Python", "number": 2}
format % d


Out[7]:
'Python has 002              Python 2 quote types.'

In [8]:
class d:
    language = 'French'
    number = 17
print(dir(d))
print(d.__dict__)
format = (
    '%(language)s has %(number)03d %(language)019s '
    '%(number)d quote types.'
)
format % d.__dict__


['__doc__', '__module__', 'language', 'number']
{'__module__': '__main__', 'number': 17, 'language': 'French', '__doc__': None}
Out[8]:
'French has 017              French 17 quote types.'

In [9]:
format % d


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-ddd3efb02867> in <module>()
----> 1 format % d

TypeError: format requires a mapping

In [ ]:
named tuples
sorted dicts