str.center
string的center方法返回以指定长度为宽度的居中字符串,使用指定字符填充,默认使用空格填充;
语法
str.center(width[,fillchar])
- 参数:width - 字符串的总宽度
fillchar - 填充的字符 - 返回值:更新后的居中字符串
示例
str = "hello"
print("str.center(10) : ",str.center(10))
print("str.center(10,'-'): ",str.center(10,'-'))
执行结果
str.center(10) : hello
str.center(10,'-'): --hello---
本文介绍Python中字符串的center方法,该方法能创建指定宽度的居中字符串,支持自定义填充字符,示例代码展示了如何使用此方法进行字符串格式化。
815

被折叠的 条评论
为什么被折叠?



