Python字符串center()方法

Python字符串center()方法以字符串宽度(width)为中心。使用指定的填充字符(fillchar)填充完成。默认填充字符(fillchar)是一个空格。

语法

以下是center()方法的语法 -

str.center(width[, fillchar])

参数

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

返回值

  • 此方法返回一个至少宽度宽的字符串,它是通过用字符fillchar填充字符串(默认为空格)创建。

示例

以下示例显示了center()方法的用法。

#!/usr/bin/python3

str = "this is string example....wow!!!"

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

当运行上面的程序,它产生以下结果 -

str.center(40, 'a') :  aaaathis is string example....wow!!!aaaa

上一篇: Python字符串 下一篇: Python列表