Game Development Reference
In-Depth Information
>>> # Python 3
>>> 'My name is {0} and I am from {1}.'.format
('Al', 'Houston')
'My name is Al and I am from Houston.'
The numbers inside the curly braces are the index of the parameters to format() . So
switching them around in the string will switch around where the parameters to format()
are placed:
>>> # Python 3
>>> 'My name is {1} and I am from {0}.'.format
('Al', 'Houston')
'My name is Houston and I am from Al.'
One nice feature is that you can use the same parameter to format() multiple times in
the string without making extra parameters to format( ):
>>> # Python 3
>>> 'My name is {0}, {0}, {0}, {0} and I am from
{1}.'.format('Jimmy Four Times', 'Houston')
'My name is Jimmy Four Times, Jimmy Four Times,
Jimmy Four Times, Jimmy Four Times and I am from
Houston.'
Along with numeric indexes, you can also put text inside the curly braces. This text can
match to keyword arguments passed to format() :
>>> # Python 3
>>> 'My name is {0} and I like {thing} and I am
from {hometown}.'.format('Al', hometown='Houston',
thing='cats')
'My name is Al and I like cats and I am from
Houston.'
 
Search WWH ::




Custom Search