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 string; in case of filling the missing bits or any other specific requirement. The solution to this kind of problems is always handy and is good if one has knowledge of it. Let’s discuss certain ways in which this can be solved.
Method #1 : Using ljust()
This task can be performed using the simple inbuilt string function of ljust in which we just need to pass no. of zeros required and the element to right pad, in this case being zero.
# 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)) |
The original string : GFG The string after adding trailing zeros : GFG0000
Method #2 : Using format()
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 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 elememts, '0' for Zero, and '<' for trailing temp = '{:<07}' res = temp. format (test_string) # print result print ( "The string after adding trailing zeros : " + str (res)) |
The original string : GFG The string after adding trailing zeros : GFG0000
Recommended Posts:
- Python | Add leading Zeros to string
- Python | sympy.trailing() method
- Python | Remove trailing empty elements from given list
- Python Program to Count trailing zeroes in factorial of a number
- Python | Remove trailing/leading special characters from strings list
- numpy.zeros() in Python
- Python | sympy.zeros() method
- Python | Remove leading zeros from an IP address
- Create a Numpy array filled with all zeros | Python
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Merge Tuple String List values to String
- Python | Check if string ends with any string in given list
- Python | Sorting string using order defined by another string
- Python | Check if a given string is binary string or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.