Understand Python String zfill() with Examples – Python Tutorial

By | August 31, 2023

Python string zfill() function can pad a string with“0” to a specified width. In this tutorial, we will introduce how to use it with some examples.

For example:

text = "tutorialexample.com"

output = text.zfill(30)
print(output)
lx = len(output)
print("output length:",len(output))

In this example, we will pad text to 30 length with “0

Run this code, we will see:

00000000000tutorialexample.com
output length: 30

However, we also can use rjust() function to get the same result.

For example:

output = text.rjust(30,"0")
print(output)
lx = len(output)
print("output length:",len(output))

Implement Python String Alignment with String ljust(), rjust(), center() Function – Python Tutorial

Run this code, we will see:

00000000000tutorialexample.com
output length: 30

Understand Python String zfill() with Examples - Python Tutorial