You are on page 1of 1

1299,

1.235235245)

Here we do not have to specify the position of the parameters. The
interpreter will replace the curly brackets based on the order of the
parameters provided. We’ll get

The price of this Apple laptop is 1299 USD and the
exchange rate is 1.235235245 USD to 1 EUR

The format() method can be kind of confusing to beginners. In fact,
string formatting can be more fanciful than what we’ve covered here, but
what we’ve covered is sufficient for most purposes. To get a better
understanding of the format() method, try the following program.

message1 = ‘{0} is easier than {1}’.format(‘Python’,
‘Java’)
message2 = ‘{1} is easier than {0}’.format(‘Python’,
‘Java’)
message3 = ‘{:10.2f} and {:d}’.format(1.234234234, 12)
message4 = ‘{}’.format(1.234234234)

print (message1)
#You’ll get ‘Python is easier than Java’

print (message2)
#You’ll get ‘Java is easier than Python’

print (message3)
#You’ll get ‘ 1.23 and 12’
#You do not need to indicate the positions of the
parameters.

print (message4)
#You’ll get 1.234234234. No formatting is done.

You might also like