Skip to content

Latest commit

 

History

History
36 lines (32 loc) · 401 Bytes

20220914144334.md

File metadata and controls

36 lines (32 loc) · 401 Bytes

zfill

Python's zfill() string method is useful to fill a string with zeros.

Alternatively you could also use an f-string:

>>> for number in range(1, 11):
...     str(number).zfill(3)
...
'001'
'002'
'003'
'004'
'005'
'006'
'007'
'008'
'009'
'010'
>>> for number in range(1, 11):
...     f"{number:0>3}"
...
'001'
'002'
'003'
'004'
'005'
'006'
'007'
'008'
'009'
'010'

#strings #f-strings