Python short course: String formatting output

 String in Python also support formatting output like below:



In [1]: '%s, eggs, and %s' % ('spam', 'SPAM')
Out[1]: 'spam, eggs, and SPAM'

In [2]: '{0}, eggs, and {1}'.format('spam', 'SPAM')
Out[2]: 'spam, eggs, and SPAM'

In [3]: '{}, eggs, and {}'.format('spam', 'SPAM')  #numbers are optional
Out[3]: 'spam, eggs, and SPAM'

Comments