# Formatting options

**URL:** <https://discuss.python.org/t/formatting-options/17429>\
**Category:** Python Help\
**Created:** [July 17, 2022, 6:27am UTC](https://discuss.python.org/t/formatting-options/17429 "2022-07-17T06:27:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohamed\_Fergany](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@Mohamed\_Fergany](https://discuss.python.org/u/Mohamed_Fergany)\
**Post date:** [July 17, 2022, 6:27am UTC](https://discuss.python.org/t/formatting-options/17429/1 "2022-07-17T06:27:01Z")

</div>

Hi everyone,

I have as quick question. In this code  
` print( 'Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)')`  
I get the 1 & 2 are for decimals but what the 5 is for?

Thanks 😊 😊

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [July 17, 2022, 7:02am UTC](https://discuss.python.org/t/formatting-options/17429/2 "2022-07-17T07:02:11Z")

</div>

The `5.2f` sets the minimum field with to 5 and the number of digits after the decimal place to 2.

So the output will be padded with spaces on the left hand side to give a total width of at least 5:

```python
'{:5.2f}'.format(1.5) # returns ' 1.50' with a leading space
'{:5.2f}'.format(100.5) # returns '100.50'

```

---

<div class="post-metadata">

**Author:** ![Mohamed\_Fergany](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@Mohamed\_Fergany](https://discuss.python.org/u/Mohamed_Fergany)\
**Post date:** [July 17, 2022, 7:11am UTC](https://discuss.python.org/t/formatting-options/17429/3 "2022-07-17T07:11:45Z")

</div>

Many thanks for your help 👍 👍

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [July 20, 2022, 11:30am UTC](https://discuss.python.org/t/formatting-options/17429/4 "2022-07-20T11:30:33Z")

</div>

For ‘coding exercise’, I’m working my way through an old book I have, converting the examples therein, into Python3 code.

This script, is a good demonstration of the formatting that you asked about.

```python
#!/usr/bin/python3

#from page 8: The C Programming Language by K&R

#:- Print a table of Fahrenheit temperatures and their centigrade or Celsius equivalents
#:- using this formula {C = (5/9)(F-32)} and a step size of 20

lower = 0
upper = 300
step = 20

fahr = lower

while fahr <= upper:
    celsius = (5.0/9.0) * (fahr-32.0)
    print(f"{fahr:4.0f} {celsius:6.1f}")
    fahr += step

```

To paraphrase from said book…

`{fahr:4.0f}` says that a floating point number is to be printed in a space at least four characters wide, with no digits after the decimal point. `{celsius:6.1f}` describes another number to occupy at least six spaces, with one digit after the decimal point.
