GeeksforGeeks App
Open App
Browser
Continue

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.

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

 `# Python3 code to demonstrate``# adding trailing zeros``# using ljust()` `# initializing string``test_string ``=` `'GFG'` `# printing original string``print``(``"The original string : "` `+` `str``(test_string))` `# No. of zeros required``N ``=` `4` `# using ljust()``# adding trailing zero``res ``=` `test_string.ljust(N ``+` `len``(test_string), ``'0'``)` `# print result``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

 `# Python3 code to demonstrate``# adding trailing zeros``# using format()` `# initializing string``test_string ``=` `'GFG'` `# printing original string``print``(``"The original string : "` `+` `str``(test_string))` `# No. of zeros required``N ``=` `4` `# using format()``# adding trailing zero``# N for number of elements, '0' for Zero, and '<' for trailing``temp ``=` `'{:<07}'``res ``=` `temp.``format``(test_string)` `# 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(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.

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.

Python3

 `s ``=` `"GFG"``num_zeros ``=` `4` `# print the original string``print``(``"The original string :"``, s)` `# add trailing zeros to the string``padded_string``=``s``i``=``0``while``(i

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.

Python3

 `str` `=` `"GFG"``n ``=` `4` `# print the original string``print``(``"The original string :"``, ``str``)` `# add trailing zeros to the string``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

 `# Python code to demonstrate``# adding trailing zeros``# using zfill()` `# initializing string``test_string ``=` `'GFG'` `# printing original string``print``(``"The original string : "` `+` `str``(test_string))` `# No. of zeros required``N ``=` `4` `# using zfill()``# adding trailing zero``res ``=` `test_string ``+` `'0'` `*` `N` `#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) as it iterates over the string.

Auxiliary Space: O(N) as we are creating a new string.

My Personal Notes arrow_drop_up