Python3 center(width, fillchar) 方法

Python3 字符串


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

语法

center()方法语法:

str.center(width[, fillchar])

参数

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

返回值

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

实例

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

#!/usr/bin/python3

str = "[www.itzixishi.com]"
print ("str.center(40, '*') : ", str.center(40, '*'))

以上实例输出结果如下:

str.center(40, '*') :  ************[www.itzixishi.com]************

Python3 字符串

本教程仅供参考学习,如用于商业带来的问题,本站概不负责。
关注公众号
关注公众号

©2020 IT自习室京ICP备20010815号