center() 返回⼀个原字符串居中,并使⽤空格填充⾄长度 width 的新字符串。默认填充字符为空格
这段代码没有你说的翻转功能,只是把一行文字按照8个一组(一句)切割成4段,然后居中输出
str.center(width[,fillchar])
:定义一个width宽度的字符串,使数据居中显示,其余部分使用fillchar进行填充,如果未定义,默认为空格填充。
参数至少为一个,最多为两个,str.center()
会报错 TypeError: center() takes at least 1 argument (0 given)
会在width中居中显示,如果不是居中,那么前短后长哦~
fillchar 默认是空格
,如果有参数,只能是一个字符
,不然会报错The fill character must be exactly one character long
如果width<str.len
会原样输出,并且也不会填充,也不会截取
help 输出的内容:
center(...)
| S.center(width[, fillchar]) -> str # 输出是一个字符串
| #s.center(宽度,填充字符) 填充字符是可选的吧
| Return S centered in a string of length width. Padding is
| done using the specified fill character (default is a space)
# fillchar
print('111111111111111111111111')#24
print('hello'.center(24))
print('hello'.center(3,'1')) # hello
print('hello'.center(10,'-'))
print('hello'.center(10,'*'))
#print('hello'.center(10,'ss')) #The fill character must be exactly one character long