Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Data Science Basics Describing Data Calculate Averages

Drew Butcher
Drew Butcher
33,160 Points

String Format

in the {:03.2f} what does the 03 do? I receive the same result with just {:0.2f}

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,154 Points

{:03.2f} says "three or more total digits, two digits after the period. Since a float will always have at least one digit in front of the period, {:03.2f}, {:02.2f}, {:01.2f}, and {:0.2f} are all equivalent.

Edit: corrected answer based on James Shi's comment.

Technically this is wrong. The 3 means at least 3 characters. The 0 means if there aren't at least 3 characters, pad zeros in front of the number to increase the amount of characters to 3. You only need {:.2f}.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,154 Points

James Shi, you are correct! Yes, the "3" does mean "three or more characters".

Part of my point was that with the ".2f" present, the a width value of three or less is equivalent:

>>> n = 3.14159265358
>>> print("{:03.2f}, {:02.2f}, {:01.2f}, and {:0.2f}".format(n, n, n, n))
3.14, 3.14, 3.14, and 3.14
>>> n = 1023.14159265358
>>> print("{:03.2f}, {:02.2f}, {:01.2f}, and {:0.2f}".format(n, n, n, n))
1023.14, 1023.14, 1023.14, and 1023.14
>>> n = 3.1
>>> print("{:03.2f}, {:02.2f}, {:01.2f}, and {:0.2f}".format(n, n, n, n))
3.10, 3.10, 3.10, and 3.10

Thanks for catching this error. I'll update the answer above.