Python3 center()方法

Python3 字串 Python3 字串

center() 方法返回一個指定的寬度 width 居中的字串,fillchar 為填充的字元,默認為空格。

語法

center()方法語法:

str.center(width[, fillchar])

參數

  • width -- 字串的總寬度。
  • fillchar -- 填充字元。

返回值

返回一個指定的寬度 width 居中的字串,如果 width 小於字串寬度直接返回字串,否則使用 fillchar 去填充。

實例

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

#!/usr/bin/python3

str = "[www.xuhuhu.com]"

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

以上實例輸出結果如下:

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

Python3 字串 Python3 字串