对字符串格式化时,如果设置了填充内容和宽度,对齐方式也必须要维护,不然会报错。
>>> print('{:*10}'.format('hello'))
Traceback (most recent call last):
File "<pyshell#299>", line 1, in <module>
print('{:*10}'.format('hello'))
ValueError: Invalid format specifier
维护对齐方式后,可以正常格式化。
>>> print('{:*<10}'.format('hello'))
hello*****
>>> print('{:*>10}'.format('hello'))
*****hello
>>> print('{:*^10}'.format('hello'))
**hello***