Pad a string with Zeros in Python

avatar

Borislav Hadzhiev

Last updated: Sep 5, 2022

banner

Photo from Unsplash

Pad a string with Zeros in Python #

Use the str.zfill() method to pad a string with zeros, e.g. result = my_str.zfill(8). The zfill() method left-fills the string with 0 digits to make it of the specified width.

main.py
# ✅ print multiple variables on separate lines (sep) my_str = 'bobby' # ✅ pad string to fixed length with leading zeros result = my_str.zfill(8) print(result) # 👉️ 000bobby # ------------------------------------------------- # ✅ pad string to fixed length with zeros (left-aligned) result = f'{my_str:0>8}' print(result) # 👉️ 000bobby # ------------------------------------------------- # ✅ pad string to fixed length with zeros (right-aligned) result = f'{my_str:0<8}' print(result) # 👉️ bobby000

The first example uses the str.zfill() method to pad the string with leading zeros.

main.py
jmy_str = 'bobby' result = my_str.zfill(8) print(result) # 👉️ 000bobby

The str.zfill method takes the width of the string and left-fills the string with 0 digits to make it of the specified width.

main.py
num = 13 result = str(num).zfill(3) print(result) # 👉️ '013' result = str(num).zfill(4) print(result) # 👉️ '0013'

Converting the number 13 to a string gives us a string with a length of 2.

Passing 3 as the width to the zfill() method means that the string will get left-filled with a single 0 digit.

If the specified width is less than or equal to the length of the original string, then the original string is returned.

Alternatively, you can use a formatted string literal.

Pad a string with Zeros using a formatted string literal #

Use a formatted string literal to pad a string with zeros, e.g. result = f'{my_str:0>8}'. You can use an expression inside of a formatted string literal where you can specify the fill character, the length of the string and the alignment.

main.py
my_str = 'bobby' # ✅ pad string with leading zeros result = f'{my_str:0>8}' print(result) # 👉️ 000bobby # ------------------------------------------------- # ✅ pad string with trailing zeros result = f'{my_str:0<8}' print(result) # 👉️ bobby000

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

main.py
name = 'bobbyhadz' extension = '.com' result = f'{name}{extension}' print(result) # 👉️ bobbyhadz.com

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

main.py
my_str = 'bobby' result = f'{my_str:0>8}' print(result) # 👉️ 000bobby

The 0 digit between the colon and the greater-than sign is the fill character.

main.py
my_str = 'bobby' result = f'{my_str:.>8}' print(result) # 👉️ ...bobby

The example above uses a period as the fill character instead of a 0.

The less-than or greater-than sign is the alignment.

The less-than sign aligns the string to the left and the greater-than sign aligns the string to the right.

The digit after the sign is the width of the string.

If you need to pad the string with trailing zeros to a fixed length, use a less-than sign.

main.py
my_str = 'bobby' result = f'{my_str:0<8}' print(result) # 👉️ bobby000

If you need to center the string, use a caret ^ symbol.

main.py
my_str = 'bobby' result = f'{my_str:0^8}' print(result) # 👉️ 0bobby00

Alternatively, you can use the str.rjust() method.

Pad a string with Zeros using str.rjust() #

Use the str.rjust() method to pad a string with zeros, e.g. result = my_str.rjust(8, '0'). The str.rjust() method will pad the beginning of the string to the specified width with zeros.

main.py
my_str = 'bobby' # ✅ pad string with leading zeros result = my_str.rjust(8, '0') print(result) # 👉️ 000bobby # ✅ pad string with trailing zeros result = my_str.ljust(8, '0') print(result) # 👉️ bobby000

The str.rjust method pads the beginning of the string to the specified width with the provided fill character.

The str.rjust method takes the following 2 arguments:

NameDescription
widthThe total length of the padded string
fillcharThe fill character to pad the string with

If you need to pad the string with trailing zeros, use the str.ljust() method instead.

main.py
my_str = 'bobby' result = my_str.ljust(8, '0') print(result) # 👉️ bobby000

The str.ljust method pads the end of the string to the specified width with the provided fill character.

The str.ljust method takes the same parameters - the width and the fill character.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.