Python String center()メソッド
例
中央に「バナナ」を入れて、20文字のスペースを占める「バナナ」という単語を印刷します。
txt = "banana"
x = txt.center(20)
print(x)
定義と使用法
このcenter()
メソッドは、指定された文字(スペースがデフォルト)を塗りつぶし文字として使用して、文字列を中央揃えにします。
構文
string.center(length, character)
パラメータ値
Parameter | Description |
---|---|
length | Required. The length of the returned string |
character | Optional. The character to fill the missing space on each side. Default is " " (space) |
その他の例
例
パディング文字として文字「O」を使用する:
txt = "banana"
x = txt.center(20, "O")
print(x)