use variable to specify format in python -
i want format string fixed width
if use following statement:
"{0:<8}".format(str(size)) #this 1 works however,
# 1 gives invalid conversion specification "{0:<width}".format(str(size)) is there anyway use variable format string?
solution 1:
use width generate string {0:<8} first
width = 8 ("{0:<%d}"%width).format(s) solution 2:
nested format:
"{0:<{1}}".format(s, width) named format maybe more readable:
"{string:<{width}}".format(string=s, width=width) solution 3:
way translate {0:<8}: .ljust(8)
"{0}".format(s.ljust(width)) i choose 3. when
1) dealing other encodings in i18n
2) print pretty
print 'a'.rjust(10, '-') ---------a
Comments
Post a Comment