Latest web development tutorials

Python3 center()方法

Python3 字符串 Python3字符串

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

語法

center()方法語法:

str.center(width[, fillchar])

參數

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

返回值

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

實例

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

#!/usr/bin/python3

str = "[www.w3big.com]"

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

以上實例輸出結果如下:

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

Python3 字符串 Python3字符串