Without any built-in methods add trailing Zeros to string

Python3




# Python3 code to demonstrate
# adding trailing zeros
 
# initializing string
test_string = 'GFG'
 
# printing original string
print("The original string : " + str(test_string))
 
# No. of zeros required
N = 4
 
# adding trailing zero
x = '0'*N
res = test_string+x
 
# print result
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):
    # repeat the zero character and add it to the end of the string
    padded_string = s + "".join(repeat("0", num_zeros))
    return padded_string
 
# test the function
s = "GFG"
num_zeros = 4
 
# print the original string
print("The original string :", s)
 
# add trailing zeros to the string
padded_string = add_trailing_zeros(s, num_zeros)
 
# print the padded string
print("The string after adding trailing zeros :", padded_string)
 
#This code is contributed by Edula Vinay Kumar Reddy


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.

Python | Add trailing Zeros to string

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.

Similar Reads

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....

Using format() to  add trailing Zeros to string

...

Without any built-in methods 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....

Using while loop and += operator

...

Using f-string (Python 3.6+)

Python3 # Python3 code to demonstrate # adding trailing zeros   # initializing string test_string = 'GFG'   # printing original string print("The original string : " + str(test_string))   # No. of zeros required N = 4   # adding trailing zero x = '0'*N res = test_string+x   # print result print("The string after adding trailing zeros : " + str(res))...

Using zfill() method

...

Contact Us