Easily build a great vocabulary without studying! Now on the App Store!
Click here for more info about the app

Python String Format Precision of Float


You can use string interpolation in combination with a dictionary for simple formatting.

print "My name is %(name)s and I have $%(change)0.2f in change in my pocket!" % { 'name': 'Sean', 'change': 00.23 }
# My name is Sean and I have $0.23 in change in my pocket!

For the precision of floats you you need to use "0.2f" after the interpolation expression, where the "2" is how many decimal places to use.

Tagged w/ #python #string interpolation