As maybe a alternative more portable [1] and efficient [2], actually you can just use str.ljust.
In [2]: '190'.ljust(8, '0')
Out[2]: '19000000'
In [3]: str.ljust?
Docstring:
S.ljust(width[, fillchar]) -> str
Return S left-justified in a Unicode string of length width. Padding is
done using the specified fill character (default is a space).
Type: method_descriptor
[1] format is not present on old python versions. format specifier was added since Python 3.0 (see PEP 3101) and Python 2.6.
[2] reverse twice is an expensive operation.
Answer from Manoel Vilela on Stack Overflow Top answer 1 of 4
62
As maybe a alternative more portable [1] and efficient [2], actually you can just use str.ljust.
In [2]: '190'.ljust(8, '0')
Out[2]: '19000000'
In [3]: str.ljust?
Docstring:
S.ljust(width[, fillchar]) -> str
Return S left-justified in a Unicode string of length width. Padding is
done using the specified fill character (default is a space).
Type: method_descriptor
[1] format is not present on old python versions. format specifier was added since Python 3.0 (see PEP 3101) and Python 2.6.
[2] reverse twice is an expensive operation.
2 of 4
56
See Format Specification Mini-Language:
In [1]: '{:<08d}'.format(190)
Out[1]: '19000000'
In [2]: '{:>08d}'.format(190)
Out[2]: '00000190'
This also works with the Formatted String Literals, or f-strings for short (New in version 3.6):
In [1]: f'{190:<08d}'
Out[1]: '19000000'
In [2]: f'{190:>08d}'
Out[2]: '00000190'
Python Reference
python-reference.readthedocs.io › en › latest › docs › str › zfill.html
zfill — Python Reference (The Right Way) 0.1 documentation
Returns the numeric string left filled with zeros in a string of specified length. ... Required. Width of the padding field. ... A sign prefix is handled correctly. The original string is returned if width is less than or equal to len(str). >>> '350'.zfill(1) '350' >>> '350'.zfill(2) '350' ...
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.zfill.html
pandas.Series.str.zfill — pandas 3.0.5 documentation - PyData |
Strings in the Series/Index are padded with ‘0’ characters on the left of the string to reach a total string length width.
Does zfill change the original string?
No. Strings are immutable, so zfill returns a new padded string.
pythonpool.com
pythonpool.com › home › tutorials › python zfill(): zero-pad strings, numbers, and signs
Python zfill(): Zero-Pad Strings, Numbers, and Signs
Should I use zfill or an f-string?
Use zfill for an existing string and sign-aware text padding; use an f-string when the source is numeric and the complete output format is known.
pythonpool.com
pythonpool.com › home › tutorials › python zfill(): zero-pad strings, numbers, and signs
Python zfill(): Zero-Pad Strings, Numbers, and Signs
Does zfill preserve a plus or minus sign?
Yes. zfill inserts zeros after a leading plus or minus sign, so '-42'.zfill(5) becomes '-0042'.
pythonpool.com
pythonpool.com › home › tutorials › python zfill(): zero-pad strings, numbers, and signs
Python zfill(): Zero-Pad Strings, Numbers, and Signs
W3Schools
w3schools.com › python › ref_string_zfill.asp
Python String zfill() Method
The zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length.
Programiz
programiz.com › python-programming › methods › string › zfill
Python String zfill()
zfill() returns a copy of the string with 0 filled to the left.
Tutorialspoint
tutorialspoint.com › python › string_zfill.htm
Python String zfill() Method
The Python String zfill() method is used to fill the string with zeroes on its left until it reaches a certain width; else called Padding. If the prefix of this string is a sign character (+ or -), the zeroes are added after the sign character
Codecademy
codecademy.com › docs › python › strings › .zfill()
Python | Strings | .zfill() | Codecademy
November 19, 2023 - The .zfill() method pads a string with zeros on the left to maintain a specific length.
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.str.zfill.html
pandas.Series.str.zfill — pandas 3.0.5 documentation
Strings in the Series/Index are padded with ‘0’ characters on the left of the string to reach a total string length width.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.strings.zfill.html
numpy.strings.zfill — NumPy v2.1 Manual
strings.zfill(a, width)[source]# Return the numeric string left-filled with zeros. A leading sign prefix (+/-) is handled by inserting the padding after the sign character rather than before. Parameters: aarray-like, with StringDType, bytes_, or str_ dtype ·
w3resource
w3resource.com › pandas › series › series-str-zfill.php
Pandas Series: str.zfill() function - w3resource
May 20, 2026 - The str.zfill() function is used to pad strings in the Series/Index by prepending '0' characters. Strings in the Series/Index are padded with '0' characters on the left of the string to reach a total string length width.