To pad strings:
>>> print('4'.zfill(3))
004
>>> print('-4'.zfill(3))
-04
To pad numbers:
>>> n = 4
>>> print(f'{n:03}') # Preferred method, python >= 3.6
004
>>> print('%03d' % n)
004
>>> print(format(n, '03')) # python >= 2.6
004
>>> print('{0:03d}'.format(n)) # python >= 2.6 + python 3
004
>>> print('{foo:03d}'.format(foo=n)) # python >= 2.6 + python 3
004
>>> print('{:03d}'.format(n)) # python >= 2.7 + python3
004
String formatting documentation.
Answer from Harley Holcombe on Stack Overflow Top answer 1 of 16
3520
To pad strings:
>>> print('4'.zfill(3))
004
>>> print('-4'.zfill(3))
-04
To pad numbers:
>>> n = 4
>>> print(f'{n:03}') # Preferred method, python >= 3.6
004
>>> print('%03d' % n)
004
>>> print(format(n, '03')) # python >= 2.6
004
>>> print('{0:03d}'.format(n)) # python >= 2.6 + python 3
004
>>> print('{foo:03d}'.format(foo=n)) # python >= 2.6 + python 3
004
>>> print('{:03d}'.format(n)) # python >= 2.7 + python3
004
String formatting documentation.
2 of 16
507
Just use the rjust method of the string object.
This example creates a 10-character length string, padding as necessary:
>>> s = 'test'
>>> s.rjust(10, '0')
>>> '000000test'
How to pad a numeric string with zeros to the right in Python? - Stack Overflow
Python strings have a method called zfill that allows to pad a numeric string with zeros to the left. In : str(190).zfill(8) Out: '00000190' How can I make the pad to be on the right ? More on stackoverflow.com
f-string and string right padding is driving me crazy
Your code works for me. >>> header = "foo" >>> endpoint = "bar" >>> ename = f'"{endpoint}"' >>> print(f'{header} {ename:<30}!') foo "bar" ! More on reddit.com
How can I easily pad a float with x zeros on the left and round/pad to y decimal places on the right?
I think this should work: "%05.2f". It's padding to a minimum overall length of 5 (which includes the decimal point) and pads with 0 instead of spaces. More on reddit.com
[deleted by user]
Regex generally isn't the right tool for this job. You can use Regex to capture the single digits, then whatever math/string function in your programming language to pad the number. But since we're always dealing with single-digit numbers, it's pretty simple. Also, did you post a question and say that you made some progress, but didn't post the Regex pattern that you came up with? Here's your answer: Regex101.com/r/HoA7YB/1 (26 steps) Regex pattern: _(\d)_ Substitution: _0\1_ A bit more complex: Regex101.com/r/HoA7YB/2 (117 steps) Regex pattern: (?<=_)(\d)(?=_) Substitution: 0\1 Even more complex: Regex101.com/r/HoA7YB/3 (131 steps) Regex pattern: (?<=_)(?=\d_) Substitution: 0 Complex isn't always better. In this case, additional complexity also increases the number of steps the Regex engine has to take when matching. Probably best to use #1 in this case. #2 and #3 both use positive lookbehinds and positive lookaheads to match but not consume characters (so we aren't replacing a character with itself). Let me know if you want me to explain further. More on reddit.com
Python Engineer
python-engineer.com › posts › pad-zeros-string
How to pad zeros to a String in Python - Python Engineer
It returns a copy of the object right justified in a sequence of length width. The default filling character is a space. However, this does not handle a sign prefix. >>> "42".rjust(5) ' 42' >>> "42".rjust(5, "0") '00042' >>> "-42".rjust(5, "0") '00-42' Converted a number to a formatted string with leading zeros by using string formatting:
PythonHow
pythonhow.com › how › pad-zeroes-to-a-string
Here is how to pad zeroes to a string in Python
This method adds zeroes to the ... pad zeroes to the left of the string, if you want to pad zeroes to the right of the string you can use the str.rjust() method or the str.ljust() method with the desired width and the padding character:...
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'
Linux Hint
linuxhint.com › python-pad-a-string-with-leading-zeros
Python Pad a String with Leading Zeros – Linux Hint
For padding a string with leading zeros in Python, the “zfill()” method can be used. It takes a number providing the required length of any Python string as an argument and adds zeros at the left side of the string until it is of the specified length.
Finxter
blog.finxter.com › home › learn python blog › python how to pad zeros to a string?
Python How to Pad Zeros to a String? - Be on the Right Side of Change
November 16, 2020 - This is called format specification in Python, or format_spec for short. In an f-string, you specify this using a colon :. Everything after the colon is a formatting option. It will not be printed to the screen. Let’s look at the syntax to pad, align and change the length of a string. We type the following in this order, after a colon : ... Example: So if we want to pad our string with zeros, i.e. fill 0, right ...
Stack Abuse
stackabuse.com › padding-strings-in-python
Padding Strings in Python
September 18, 2023 - The zfill() function performs very similarly to using rjust() with zero as the specified character. It left-pads the given string with zeros until the string reaches the specified length.
GeeksforGeeks
geeksforgeeks.org › add-padding-to-a-string-in-python
Add padding to a string in Python - GeeksforGeeks
December 24, 2024 - We can manually add padding by calculating the required number of characters and concatenating them to the string. ... s = "Python" width = 10 # Add padding padded = " " * (width - len(s)) + s # Right-align print(padded) ... The " " * (width - len(s)) creates the required padding by repeating the space character. This padding is concatenated with the string 's' to achieve the desired alignment.
Codecademy
codecademy.com › docs › python › strings › .zfill()
Python | Strings | .zfill() | Codecademy
November 19, 2023 - Returns a string with zeros padding the left side based on the integer given.
TutorialsPoint
tutorialspoint.com › article › how-to-get-a-string-left-padded-with-zeros-in-python
How to get a string left padded with zeros in Python?
March 24, 2026 - Use {:0>width} where 0 is the fill character, > means right-align (left-pad), and width is the total width. text = "Python" padded = '{:0>10}'.format(text) print("Original string:", text) print("Padded string:", padded) ...