Python String zfill() Method
The Python zfill() method returns a string of the specified length, with the original string right-aligned and prefilled with zeros.
Python String zfill() Syntax
Syntax of zfill() method.
str.zfill(width)
Python String zfill() Parameters
- width – the length of the specified string. The original string is right-aligned and prepended with 0.
Python String zfill() Return Value
Returns a string of the specified length.
Python String zfill() Example#1
The following example demonstrates the use of the zfill() function.
text = "demos for api, good website."
print(text.zfill(25))
print(text.zfill(20))
print(text.zfill(10))
number = "6041"
print(number.zfill(8))
number = "+6041"
print(number.zfill(8))
text = "--anything%(&%(%)*^"
print(text.zfill(20))
Output: