How to use while loop and += operator In Python
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<num_zeros): padded_string + = "0" i + = 1 #print the padded string 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.
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.
Contact Us