Open In App

Add Leading Zeros to String – Python

Last Updated : 17 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a string and we need to add a specified number of leading zeros to beginning of the string to meet a certain length or formatting requirement.

Using rjust() 

rjust() function in Python is used to align a string to the right by adding padding characters (default is a space) to the left and using this function you can specify the total width of the resulting string and the character used for padding.

Python
s = 'GFG'

# using rjust() adding leading zero
res = s.rjust(4 + len(s), '0')

print("The string after adding leading zeros : " + str(res))

Output
The string after adding leading zeros : 0000GFG

Explanation: rjust(4 + len(s), '0') calculates the total width (4 zeros + string length) and pads the string with zeros to the left.

Using zfill() 

zfill() function in Python pads a string with leading zeros until it reaches the specified width and if the specified width is less than the string’s length, the string remains unchanged.

Python
s = 'GFG'

# Using zfill() to add leading zeros
res = s.zfill(4 + len(s))

print("The string after adding leading zeros: " + res)

Output
The string after adding leading zeros: 0000GFG

Explanation: zfill() pads the string with zeros to reach the specified width. Here, 4 + len(s) ensures the string s is padded with 4 zeros.

Using string concatenation

In this method, we are multiplying the 0 as a string with the number of zeros required and using Python concatenation to merge them all.

Python
s = 'GFG'

# Adding leading zeros
res = '0' * 4 + s

print("The string after adding leading zeros: " + res)

Output
The string after adding leading zeros: 0000GFG

Using String Formatting

We can directly add required no of zeros using python’s string formatting.

Python
s = 'GFG'

# Add leading zeros using string formatting
res = "{:0>{width}}".format(s, width=4 + len(s))

print("The string after adding leading zeros: " + res)

Output
The string after adding leading zeros: 0000GFG

Explanation: format string "{:0>{width}}" tells Python to pad the string s with zeros (0) to match the specified total width. The width is calculated as 4 + len(s), ensuring that four zeros are added before the original string.

Using while Loop and += Operator

In this approach, we are using a while loop to manually append the required number of zeros to the string s. The loop runs N times, adding one zero per iteration, and then the original string s is concatenated to the result.

Python
s = "GFG"
N = 4

# Add leading zeros using a while loop
res = ""
i = 0
while i < N:
    res += "0"
    i += 1
res += s

print("The string after adding leading zeros: " + res)

Output
The string after adding leading zeros: 0000GFG

Using string.ljust()

string.ljust(width, fillchar) method in Python pads the string with the specified fillchar (default is a space) on the left side until the string reaches the specified width.

Python
s = "GFG"
N = 4

# Using ljust() to add leading zeros
res = s.ljust(N + len(s), '0')

print("The string after adding leading zeros: " + res)

Output
The string after adding leading zeros: GFG0000

Using itertools.repeat() and join() method

itertools.repeat("0", N) repeats the string '0' for N times and the join() method is used to concatenate the repeated zeros into a single string, which is then concatenated with the original string s to add the leading zeros.

Python
import itertools

s = "GFG"
N = 4

# Adding leading zeros
res = "".join(itertools.repeat("0", N)) + s

print("The string after adding leading zeros: " + res)

Output
The string after adding leading zeros: 0000GFG


Next Article
Practice Tags :

Similar Reads