Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

don't understand alignment symbols and how this piece of code works

im taking a summer course for python and came across this python code:

num1 = input( )  # input will be 10

num2 = input( )  # input will be 20

num3 = num1 + num2

print (f'The sum is {num3:<50}')

what does this last piece do inside the curly brace {num3: <50}? What does that <50 mean??

Any help appreciated, thanks !

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It means the value is left aligned within the given space of 50 chars which makes little sense if nothing is coming after:

>>> num3 = 3.14
>>> f'The sum is {num3:<50} hello')
'The sum is 3.14                                             hello'

From the docs on string format syntax on "<":

Forces the field to be left-aligned within the available space (this is the default for most objects).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading