C has printf("%Xd", Y);
, which just prints the integer X and makes it take Y spaces on the console window.
For example:
printf("%3d", 10);
console: " 10"`
printf("%5d", 5);
console: " 5"
How do I use this in python 3?
This print("{0:10d}".format(5))
will print 5 after 9 blanks.
For more reference on formatting in python refer this.
In general case, we could use string .format()
:
>>> '{:<30}'.format('left aligned')
'left aligned '
>>> '{:>30}'.format('right aligned')
' right aligned'
>>> '{:^30}'.format('centered')
' centered '
>>> '{:*^30}'.format('centered') # use '*' as a fill char
'***********centered***********'
We could also do that with f strings for python >3.6
>>> f"{'left aligned':<30}"
'left aligned '
>>> f"{'right aligned':>30}"
' right aligned'
>>> f"{'centered':^30}"
' centered '
>>> f"{'centered':*^30}" # use '*' as a fill char
'***********centered***********'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With