Python 3 教程 Python3 center()方法

travon · 2022-03-13 18:09:53 · 热度: 9

center() 方法返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。

语法

center()方法语法:

str.center(width[, fillchar])

参数

  • width -- 字符串的总宽度。
  • fillchar -- 填充字符。

返回值

返回一个指定的宽度 width 居中的字符串,如果 width 小于字符串宽度直接返回字符串,否则使用 fillchar 去填充。

实例

以下实例展示了center()方法的实例:

实例

#!/usr/bin/python3

str = "[codercto]"

print ("str.center(40, '*') : ", str.center(40, '*'))

以上实例输出结果如下:

str.center(40, '*') :  ****************[codercto]****************

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册