Python String zfill()メソッド
例
10文字の長さになるまで、文字列をゼロで埋めます。
txt = "50"
x = txt.zfill(10)
print(x)
定義と使用法
このzfill()
メソッドは、指定された長さに達するまで、文字列の先頭にゼロ(0)を追加します。
lenパラメーターの値が文字列の長さよりも小さい場合、入力は行われません。
構文
string.zfill(len)
パラメータ値
Parameter | Description |
---|---|
len | Required. A number specifying the desired length of the string |
その他の例
例
10文字の長さになるまで、文字列をゼロで埋めます。
a = "hello"
b = "welcome to the jungle"
c = "10.000"
print(a.zfill(10))
print(b.zfill(10))
print(c.zfill(10))