Prev | Index | NextString Formatting in Python
- Often you'll want some control over how your values are printed out
- Python provides C-like format strings
- Uses the special % operator after a string, followed by a value, or tuple of values to fill it in
- Works just fine in assignment too!
>>> "%s %s!" % ('hello', 'world')
'hello world!'
>>> ugly_price = 24.32203952
>>> formatted_price = "%.2f" % ugly_price
>>> print formatted_price
24.32
An Introduction To Python