The zfill()
method in Python is a string method that pads a numeric string on the left with zeros to fill a specified width. It takes one argument, which is the width of the resulting padded string. If the original string already exceeds the specified width, the method returns the original string unchanged.
Parameter Values
Parameter | Description |
---|---|
width | An integer value specifying the length of the resulting string after zfill. |
Return Values
The zfill()
method from Python's string methods returns a new str
with '0' characters padded to the left.
How to Use zfill()
in Python
The zfill()
method adds zeros at the beginning of a string to make it a specified length.
'123'.zfill(5)
If the string is already longer than the specified length, the zfill()
method will not add any zeros.
'hello'.zfill(5)
The zfill()
method is commonly used for formatting numbers with leading zeros.
'42'.zfill(3)