Python String Light()メソッド
例
「バナナ」という単語の左寄せバージョンである20文字を返します。
txt = "banana"
x = txt.ljust(20)
print(x, "is my
favorite fruit.")
注:結果として、バナナという単語の右側には実際には14個の空白があります。
定義と使用法
このljust()
メソッドは、指定された文字(スペースがデフォルト)を塗りつぶし文字として使用して、文字列を左揃えにします。
構文
string.ljust(length, character)
パラメータ値
Parameter | Description |
---|---|
length | Required. The length of the returned string |
character | Optional. A character to fill the missing space (to the right of the string). Default is " " (space). |
その他の例
例
パディング文字として文字「O」を使用する:
txt = "banana"
x = txt.ljust(20, "O")
print(x)