Python String Formatting New and Old Style

English: Python logo Deutsch: Python Logo

English: Python (Photo credit: Wikipedia)

Python programmers are presented with a couple different options to format output strings. There is an older Python 2.x style, and a newer Python 3.x style. For backwards compatibility, Python 3.x supports the older formatting style. Official documentation is available for both styles:

You can find extensive examples online for virtually any string formatting scenario. Here is one example for simple fixed point formatting of a floating point number (two decimal places).

# NOTE: Examples using python 3.x print() function
print("Floating Point Formatting %.2f modulo operator" % 3.14159)
print("Floating Point Formatting {0:.2f} format method".format(3.14159))
Advertisements

About notesbytom

Keeping technology notes on WordPress.com to free up my mind to solve new problems rather than figuring out the same ones repeatedly :-).
This entry was posted in Python and tagged , . Bookmark the permalink.

One Response to Python String Formatting New and Old Style

  1. zoosuck says:

    I think the older one is better, because it is C style.But the new is ok,too 3.x style.BTW the 3.x is not as Popular as 2.x. LOL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s