Python 字符串 center() 方法
实例
打印单词 "banana",占用 20 个字符,并使 "banana" 居中:
txt = "banana"x = txt.center(20)print(x)
定义和用法
center() 方法将使用指定的字符(默认为空格)作为填充字符使字符串居中对齐。
语法
string.center(length, character)
参数值
| 参数 | 描述 |
|---|---|
| length | 必需。所返回字符串的长度。 |
| character | 可选。填补两侧缺失空间的字符。默认是 " "(空格)。 |
更多实例
使用字母 "O" 作为填充字符:
txt = "banana"x = txt.center(20, "O")print(x)