DAY 006--str(对齐方法总结)
str对齐方法
1、str.center(width,fullchar=None)
- 字符串内容居中,不足的长度用fullchar来代替
s1="abcdef"
print(s1.center(20,"-"))
-------abcdef-------
2、str.ljust(width,fullchar=None)
- 字符串内容左对齐,不足的长度用fullchar来代替
s1="abcdef" print(s1.ljust(20,"*")) abcdef**************
3、str.rjust(width,fullchar=None)
- 字符串内容右对齐,不足的长度用fullchar来代替
s1="abcdef" print(s1.rjust(20,"*")) **************abcdef
4、format(str,"fullchar[>\<\^]width")
- 右对齐,format(str,"fullchar<width)
- 左对齐,format(str,"fullchar>width)
- 居中对齐,format(str,"fullchar^width)
s1="abcdef" print(format(s1,"*<20")) abcdef************** print(format(s1,"*>20")) **************abcdef print(format(s1,"*^20")) *******abcdef*******
Mark on 2018.04.07