original_string = "123" padded_string = original_string.zfill(5) print(padded_string)
00123
You can pad zeroes to a string in Python by using the str.zfill() method. This method adds zeroes to the left of the string until it reaches the specified width.
You can also use the string formatting method in python:original_string = "123" padded_string = "{:05}".format(original_string) print(padded_string)
Both the above methods will pad zeroes to the left of the string, if you want to pad zeroes to the right of the string you can use the str.rjust() method or the str.ljust() method with the desired width and the padding character:
original_string = "123" padded_string = original_string.rjust(5, '0') print(padded_string)This will output:
12300
The first argument of the method is the width of the final string, and the second argument is the padding character, in this case, it is '0'.
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.