Add Leading Zeros to String – Python
Last Updated :
17 Jan, 2025
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))
OutputThe 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)
OutputThe 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)
OutputThe 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)
OutputThe 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)
OutputThe 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)
OutputThe 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)
OutputThe string after adding leading zeros: 0000GFG
Similar Reads
Add trailing Zeros to string-Python
Add trailing zeros to string in Python involves appending a specific number of zero characters ('0') to the end of a given string. This operation is commonly used in formatting, padding, or aligning strings for display or data processing. For example, adding 4 zeros to "GFG" should result in "GFG000
3 min read
Python | Padding a string upto fixed length
Given a string, the task is to pad string up to given specific length with whitespaces. Let's discuss few methods to solve the given task.Method #1: Using ljust() C/C++ Code # Python code to demonstrate # pad spaces in string # upto fixed length # initialising string ini_string = "123abcjw
3 min read
Python - Words Lengths in String
We are given a string we need to find length of each word in a given string. For example, we are s = "Hello world this is Python" we need to find length of each word so that output should be a list containing length of each words in sentence, so output in this case will be [5, 5, 4, 2, 6]. Using Lis
2 min read
Python | Add one string to another
The concatenation of two strings has been discussed multiple times in various languages. But the task is how to add to a string in Python or append one string to another in Python. Example Input: 'GFG' + 'is best' Output: 'GFG is best' Explanation: Here we can add two string using "+" operator in Py
5 min read
How to Create String Array in Python ?
To create a string array in Python, different methods can be used based on the requirement. A list can store multiple strings easily, NumPy arrays offer more features for large-scale data and the array module provides type-restricted storage. Each method helps in managing collections of text values
2 min read
Convert tuple to string in Python
The goal is to convert the elements of a tuple into a single string, with each element joined by a specific separator, such as a space or no separator at all. For example, in the tuple ('Learn', 'Python', 'Programming'), we aim to convert it into the string "Learn Python Programming". Let's explore
3 min read
How to Pad a String to a Fixed Length with Zeros in Python
Padding a string to a fixed length with zeros in Python is basically adding leading zeros until it reaches the desired size. Using zfill() MethodThe simplest way to pad a string with zeros is by using Pythonâs built-in zfill() method. This method adds zeros to the left of a string until it reaches t
2 min read
Do Python Strings End in a Terminating NULL
When working with strings in programming, especially for those familiar with languages like C or C++, it's natural to wonder whether Python strings are terminated with a NULL character (\0). The short answer is no, Python strings do not use a terminating NULL character to mark their end. Python stri
4 min read
Integer to Binary String in Python
We have an Integer and we need to convert the integer to binary string and print as a result. In this article, we will see how we can convert the integer into binary string using some generally used methods. Example: Input : 77Output : 0b1001101Explanation: Here, we have integer 77 which we converte
4 min read
How to find length of a string in Python
In this article, we will learn how to find length/size of a string in Python. To find the length of a string, you can use built-in len() method in Python. This function returns the number of characters in the string, including spaces and special characters. [GFGTABS] Python s = "GeeksforGeeks
2 min read