Python center()メソッド

Python 字符串Python 文字列


説明

Python center() は、元の文字列を中央に配置し、長さ width になるまでスペースで埋めた新しい文字列を返します。デフォルトの埋め文字はスペースです。

構文

center()メソッドの構文:

str.center(width[, fillchar])

パラメータ

  • width -- 文字列の全体の幅。
  • fillchar -- 埋め文字。

戻り値

このメソッドは、元の文字列を中央に配置し、長さ width になるまでスペースで埋めた新しい文字列を返します。

実例

以下の例は center() メソッドを示しています。

>>>str = 'サンプル' >>> str.center(20, '*') '*******サンプル*******' >>> str.center(20) ' サンプル ' >>>

Python 字符串Python 文字列

その他の拡張