Pad Strings and Numbers with Zeros in Python (Zero-padding)

Modified: | Tags: Python, String

This article explains how to perform zero-padding on various data types such as strings (str) and integers (int) in Python.

Right-justified and zero-filled: zfill()

str has the zfill() method.

Specify the length of the returned string. The original string will be right-justified, and the left side will be filled with zeros.

s = '1234'

print(s.zfill(8))
# 00001234

print(type(s.zfill(8)))
# <class 'str'>

If a value smaller than the length of the original string is given, the original string will be returned as it is.

print(s.zfill(3))
# 1234

If a leading sign prefix (-, +) exists, the string is filled with zeros after the sign.

s = '-1234'
print(s.zfill(8))
# -0001234

s = '+1234'
print(s.zfill(8))
# +0001234

Non-numeric strings are treated the same way.

s = 'abcd'
print(s.zfill(8))
# 0000abcd

Because zfill() is a method of str, it cannot be called from objects of other types, such as int. To pad an integer with zeros, first convert it to a string using str(), then call zfill().

i = 1234

print(type(i))
# <class 'int'>

# print(i.zfill(8))
# AttributeError: 'int' object has no attribute 'zfill'

print(str(i).zfill(8))
# 00001234

Right-justified, centered, left-justified: rjust(), center(), ljust()

If you want to center or left-justify in addition to right-justify and zero-fill, use the center() and ljust() methods of str.

For these methods, the first argument is the length of the string to be returned, and the second argument is '0' for zero-padding.

s = '1234'

print(s.rjust(8, '0'))
# 00001234

print(s.center(8, '0'))
# 00123400

print(s.ljust(8, '0'))
# 12340000

Note that a leading sign prefix (-, +) is not considered.

s = '-1234'

print(s.rjust(8, '0'))
# 000-1234

print(s.center(8, '0'))
# 0-123400

print(s.ljust(8, '0'))
# -1234000

Since these are also methods of str, to apply them to numbers, convert a number to a string with str() before calling them.

Format: format(), f-strings

The built-in format() function and the format() method of str provide various formatting options, including zero-padding.

Examples of right-justified and zero-filled with the format() method of str are shown here. For other examples and detailed usage, see the following articles.

For strings, use 0>[STRING_LENGTH] as the format string. A leading sign prefix (+, -) is not considered.

s = '1234'

print('Zero Padding: {:0>8}'.format(s))
# Zero Padding: 00001234

s = '-1234'

print('Zero Padding: {:0>8}'.format(s))
# Zero Padding: 000-1234

Since a leading sign prefix is considered for numbers as described later, if you want to consider the sign, use int() to convert a string to an integer. In that case, use 0[STRING_LENGTH].

s = '-1234'

print('Zero Padding: {:08}'.format(int(s)))
# Zero Padding: -0001234

For numbers, use 0[STRING_LENGTH]. A leading sign prefix is considered. You can also do various operations, e.g., convert to hexadecimal and zero-padding.

i = 255

print('Zero Padding: {:08}'.format(i))
# Zero Padding: 00000255

print('Zero Padding: {:08x}'.format(i))
# Zero Padding: 000000ff

i = -1234

print('Zero Padding: {:08}'.format(i))
# Zero Padding: -0001234

In Python 3.6 or later, you can also use f-strings for a more concise representation.

print(f'Zero Padding: {i:08}')
# Zero Padding: -0001234

The % operator for strings

You can use the % operator with a string to achieve formatted output.

Note that f-strings and format() are recommended in the official documentation.

Note: The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals, the str.format() interface, or template strings may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility. Built-in Types - printf-style String Formatting — Python 3.11.3 documentation

For integers, use %0[STRING_LENGTH]d

i = 1234

print('Zero Padding: %08d' % i)
# Zero Padding: 00001234

i = -1234

print('Zero Padding: %08d' % i)
# Zero Padding: -0001234

For strings, it is filled with spaces. If you want to fill it with zeros, convert it to an integer with int().

s = '1234'

print('Zero Padding: %08s' % s)
# Zero Padding:     1234

print('Zero Padding: %08d' % int(s))
# Zero Padding: 00001234

Related Categories

Related Articles