apply formatting to numbers using str.format()
num = 32.554865
print("I own {pct}% of the company".format(pct=num))
I own 32.554865% of the company
I own 32.55% of the company
print("The approximate population of {0} is {1}".format("India",1324000000))
The approximate population of India is 1324000000
The approximate population of India is 1,324,000,000
Your bank balance is $12,345.68
pop_millions = [
["China", 1379.302771],
["India", 1281.935991],
["USA", 326.625791],
["Indonesia", 260.580739],
["Brazil", 207.353391],
]
temp_str = "The population of {0} is {1:,.2f} million"
for item in pop_millions:
print(temp_str.format(item[0], item[1]))
The population of China is 1,379.30 million
The population of India is 1,281.94 million
The population of USA is 326.63 million
The population of Indonesia is 260.58 million
The population of Brazil is 207.35 million