how to use alignment and leading zeros here (within the f-string)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to use alignment and leading zeros here (within the f-string)?

a = 10 b = 20 expected output: 000010 000020 .............. .............. #using f-string, <, and 06 # how to put 6 ({a:06}, {b:06}) leading zeros for each num. : # f"{a:<20} {b:<20}"

29th Oct 2018, 11:41 AM
Tincture
3 Answers
+ 1
06 means that your number is filled up with zeros and the whole thing spreads on 6 letters. The number itself is right-aligned (otherwise you wouldn't have zeros on the left side), but the thing as a whole is not really aligned anymore... Is this about a space between the columns? Either you can fill up the f-string with spaces like {} {} - or you could use two f-strings and use the str justification methods, for example: print(f'{a:06}'.ljust(8), f'{b:06}'.ljust(8)) So this would mean that you rjust a number on a space of 6, filling up with zeros, and then you take this thing and ljust it on a width of 8.
29th Oct 2018, 1:14 PM
HonFu
HonFu - avatar
0
I just tried this, seems to work! (Or maybe I haven't understood what you're aiming at?) a=2 b=4 print(f'{a:06} {b:06}')
29th Oct 2018, 11:55 AM
HonFu
HonFu - avatar
0
I want to use both leading zeros and left alignment, not just leading zeros. HonFu
29th Oct 2018, 12:49 PM
Tincture