An elegant way to print "==== middle justified title ====" in python
As I know there are several elegant ways to print left and right padded lines. like this
str = "left_justified"
str.ljust(20, '0');
or
print "{0:{1}<20}".format(str, "=")
the result will be
left_justified=====
What is the best way to print a mid-justified row with padding
+3
jinhwan
source
to share
3 answers
>>> "hello".center(50, '=')
'======================hello======================='
+10
icyrock.com
source
to share
You missed ^
:
s = 'centered'
print "{0:{1}^20}".format(s, "=")
# -> ======centered======
I also took the liberty of renaming your variable str
to something that the inline doesn't hide str
.
+5
bernie
source
to share
>>> termwidth, fillchar = 78, '='
>>> print ' middle justified title '.center(termwidth, fillchar)
=========================== middle justified title ===========================
+2
wim
source
to share