Python | Add trailing Zeros to string
Last Updated :
14 Mar, 2023
Sometimes, we wish to manipulate a string in such a way in which we might need to add additional zeros at the end of the string; in case of filling the missing bits or any other specific requirement. The solution to this kind of problem is always handy and is good if one has knowledge of it. Let’s discuss certain ways in which this can be solved.
Using ljust() to add trailing Zeros to the string
This task can be performed using the simple inbuilt string function of ljust in which we just need to pass the number of zeros required in Python and the element to right pad, in this case being zero.
Python3
test_string = 'GFG'
print ( "The original string : " + str (test_string))
N = 4
res = test_string.ljust(N + len (test_string), '0' )
print ( "The string after adding trailing zeros : " + str (res))
|
Output
The original string : GFG
The string after adding trailing zeros : GFG0000
Output:
The original string : GFG
The string after adding trailing zeros : GFG0000
Time Complexity: O(n)
Auxiliary Space: O(n)
Using format() to add trailing Zeros to string
String formatting using the format() function can be used to perform this task easily, we just mention the number of elements total, element needed to pad, and direction of padding, in this case right.
Python3
test_string = 'GFG'
print ( "The original string : " + str (test_string))
N = 4
temp = '{:<07}'
res = temp. format (test_string)
print ( "The string after adding trailing zeros : " + str (res))
|
Output
The original string : GFG
The string after adding trailing zeros : GFG0000
Time complexity: O(1) as it does not involve any loops or iterations.
Auxiliary space:O(1) as it only involves a fixed amount of memory used to store the input string, the integer value N, and the formatted output string.
Without any built-in methods add trailing Zeros to string
Python3
test_string = 'GFG'
print ( "The original string : " + str (test_string))
N = 4
x = '0' * N
res = test_string + x
print ( "The string after adding trailing zeros : " + str (res))
|
Output
The original string : GFG
The string after adding trailing zeros : GFG0000
Time complexity: O(N), where N is the number of zeros required to be added to the original string.
Auxiliary space: O(N)
Using repeat() to add trailing Zeros to string:
Here is an alternative approach using the repeat() function from the itertools module:
Python3
from itertools import repeat
def add_trailing_zeros(s, num_zeros):
padded_string = s + " ".join(repeat(" 0 ", num_zeros))
return padded_string
s = "GFG"
num_zeros = 4
print ( "The original string :" , s)
padded_string = add_trailing_zeros(s, num_zeros)
print ( "The string after adding trailing zeros :" , padded_string)
|
Output
The original string : GFG
The string after adding trailing zeros : GFG0000
This code defines a function add_trailing_zeros() that takes in a string s and an integer num_zeros and returns a new string with num_zeros number of zeros added to the end of the string.
The function uses the repeat() function from the itertools module to create an iterable of num_zeros zeros, and then converts it to a string using the join() method. The resulting string is then concatenated to the end of the input string using the + operator.
The code then tests the function by initializing the string s to “GFG” and the number of zeros num_zeros to 4. It prints the original string and then calls the add_trailing_zeros() function to add trailing zeros to the string.
Time complexity: O(n), where n is the length of the string.
Auxiliary Space: O(n), since a new string is created to store the padded string.
Using while loop and += operator
Python3
s = "GFG"
num_zeros = 4
print ( "The original string :" , s)
padded_string = s
i = 0
while (i<num_zeros):
padded_string + = "0"
i + = 1
print ( "The string after adding trailing zeros :" , padded_string)
|
Output
The original string : GFG
The string after adding trailing zeros : GFG0000
Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(n), since a new string is created to store the padded string.
Using f-string (Python 3.6+)
Python3
str = "GFG"
n = 4
print ( "The original string :" , str )
str = f "{str}{'0'*n}"
print ( "The string after adding trailing zeros :" , str )
|
Output
The original string : GFG
The string after adding trailing zeros : GFG0000
Time complexity: O(n), where n is the length of the string.
Auxiliary Space: O(1)
Using zfill() method
- N no. of zeros to add
- Using zfill() inbuild method to add the zeros to the string.
- Printing the updated string
Python3
test_string = 'GFG'
print ( "The original string : " + str (test_string))
N = 4
res = test_string + '0' * N
print ( "The string after adding trailing zeros : " + str (res))
|
Output
The original string : GFG
The string after adding trailing zeros : GFG0000
Time complexity: O(N) as it iterates over the string.
Auxiliary Space: O(N) as we are creating a new string.
Share your thoughts in the comments
Please Login to comment...