Skip to content

Instantly share code, notes, and snippets.

@shihpeng
shihpeng / gist:115dc911a67963ea3af8
Created September 29, 2014 09:51
Best way to do Python string format
# Python 2.6+
# After Python 2.6, you can choose to use .format() function or the old %.
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'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'