Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,423 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,423 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.