Here we will discuss how to **add zeros before a number in Python**. Adding simply means the addition of insignificant characters to the left or right side of a given string and also we are going to cover below following topics.

- Python add zeros before a number
- Python add zeros in front of a number
- Python add zeros before integer
- Python add trailing zeros to number
- Python add leading zeros to number
- Python add zeros before the number with user input
- Python add a number to list

## Python add zeros before the number

- In this section, we will discuss how to add zeros before the number in Python.
- Padding is added to the left side of a string by the rjust() function. Padding, to put it simply, is the addition of insignificant characters to the left or right side of a given string without changing the original string’s meaning. To display leading zeros in a Python program, we can utilize string padding.
- Two parameters make up the rjust() function: width and fill char. Out of these two, the width argument is required and used to indicate the length of the supplied string following the padding procedure.
- Here is an example program that explains how to use each of the methods in this example to display the numbers 1 and 10 with leading zeros. The rjust() function allows the user to directly choose how much padding is needed.

**Example**:

```
input_val = [4, 60]
for z in input_val:
print (str(z).rjust(4, '0'))
```

In this example we have created a list and assign the integer values to it. Next, we iterate values by using for loop and also we used the rjust() function and this function will add the zero values before the integer number.

Here is the implementation of the following given code

This is how to **add zeros before the number in Python**.

Read How to find perfect number in Python

## Python add zeros in front of a number

- In this example, we will discuss how to add zeros in front of a number in Python.
- By using the zfill() function we can easily use to pad a string with zeros to a given length.
- The length of the string that needs paddingâ€”not necessarily the total number of zeroesâ€”is the parameter to zfill. Thus, in the prescriptive, if your string already contains 5 characters, it won’t append any zeros.

**Example**:

```
new_val = 26
new_output = str(new_val).zfill(3)
print(new_output)
```

In the following given code we have define the variable and assign the integer number. Next we used the zfill() function can be used to pad a string with leading zeros by adding 0s to the start of the string.

Here is the screenshot of the following given code

As you can see in the screenshot we have understood how to add zeros value in front of a number in Python.

## Python add zeros before integer

- In this section, we will discuss how to
**add zeros before integer numbers in Python**. - The
**zfill()**method can be used to pad a string with leading zeros by adding 0s to the beginning of the string to make it the desired length. In effect, we apply the left padding technique, which outputs the string with padding and accepts the size of the string as an argument. - In this example, we have to add zeros before the integer number. The length of the string you want to pad, not necessarily the total number of zeroes to add, is the parameter to zfill.

**Example**:

```
new_number = 56
new_output = str(new_number).zfill(5)
print(new_output)
```

In the following given code first, we declare an integer value and assigned it to a ‘new_number’ variable.

Next, we used the str class to convert the number to a string along with that we used the zfill() function, and within this function, we passed the integer value as an argument that shows how many zeros we want to add before the number.

Here is the implementation of the following given code

This is how we can add zeros before integer numbers in Python.

Read How to reverse a number in Python

## How to add trailing zeros to a number in Python

- Here we will discuss how to
**add trailing zeros to numbers in Python**. - To perform this particular task we are going to use the ljust() Method. Python’s ljust() function left aligns the string and inserts fillchars into any empty spaces. This method outputs a new string that is left-justified and contains fillchars.
- This method takes two parameters and returns the value with the extended length.

**Syntax**:

Here is the Syntax of the ljust() function in Python.

`string.ljust(width, char)`

- It consists of a few parameters
**width**: This option specifies the overall length of the needed string. The function returns the same string if it is the same length as the current string or less.**char:**It is an optional parameter that is used to characters fill the remaining space in the string.

**Example**:

Let’s take an example and check how we can add trailing zeros to numbers in Python.

**Source Code**:

```
Country_name = 'USA'
# It displays how many zeros are required
m = 5
# adding trailing zero
new_val = '0'*m
new_output = Country_name+new_val
# Display the result
print("String after adding trailing zeros:", new_output)
```

In the following given code first we define a variable ‘Country_name’ and assign the string value ‘USA’. Next, we declare a variable that defines how many zeros we have required and then add trailing zero by ‘0’*m. After executing the code it will display the zero values before the number.

Here is the implementation of the following given code

This is how to add trailing zeros to a number in Python.

Read Python Program for even or odd

## Python add leading zeros to a number

- In this example, we will discuss how to add leading zeros to numbers in Python.
- Using the
**str.format()**function is yet another approach to applying string formatting in Python. It uses curly brackets to indicate where in the print statement the variables need to be substituted. - The
**str.format()**function, can be used in all following Python releases up to Python 3.5. Due to this function’s extremely effective handling of complex string formatting, programmers highly recommend using it when implementing string formatting.

**Example**:

```
new_val="{:02d}".format(5)
print(new_val)
```

In the above code, we have used str.format() function and within this function, we passed the integer value as an argument.

Here is the implementation of the following given code

This is how to add leading zeros to a number in Python.

Read Complex Numbers in Python

## Python add zeros before the number with user input

- In this example, we will add zeros before the number with user input in Python TensorFlow.
- In Python, the input() and print() functions are used to receive input from users and produce output that is displayed on the screen. Users can supply the application with any information in the form of texts or numbers by using the input() function.
- In this example, we will also use the zfill() function and the
**zfill()**method can be used to pad a string with leading zeros by adding 0s to the beginning of the string to make it the desired length. In effect, we apply the left padding technique, which outputs the string with padding and accepts the size of the string as an argument.

**Example**:

```
new_val = input("Enter the string:")
print("Input string is : ",new_val)
result = new_val.zfill(10)
print("Padded string is : ",result)
```

In the following given code first, we take the input() function from the input user and then used the **str.zfill() **function, within this function, we passed the integer value as an argument and it will be added zeros before the number.

Here is the Screenshot of the following given code

This is how we can add the zero values before the ‘NewYork’ string value.

Read Python Palindrome Program With Examples

## Python add a number to list

- In this section, we will discuss how to add a number to a list in Python.
- A list that has integers added to it will have its values increased as a result. For instance,
**[56, 92, 18]**is the result of adding the integers**[56, 92, 18] to [0, 0, 0]**. - To get a list of tuples with the index and integer included for each member of integers to add, call enumerate(integers to add). Each value from integers to add should be added to the value at the corresponding index of the original list by iterating through this list using a for-loop.

**Example**:

Let’s take an example and check how to add a number to a list in Python.

**Source Code**:

```
original_list = [0, 0, 0]
add_numbers = [56, 92, 18]
for m, z in enumerate(add_numbers):
add_numbers[m] += z
print(add_numbers)
```

In the above code first, we will create an original list and within this list, we assign the zero values. Next, we declare the variable and assign the integer values.

Here is the Screenshot of the following given code

In this article we have discussed how to add zeros before numbers in Python. And also we have covered below following topics.

- Python adds zeros before the number
- Python add zeros in front of a number
- Python add zeros before integer
- Python add trailing zeros to number
- Python add leading zeros to number
- Python add zeros before the number with user input
- Python add a number to list

You may like the following Python tutorials:

- Armstrong number in Python
- How to trim a string in Python
- How to read video frames in Python
- Python program to print element in an array
- Python binary tree implementation
- Python concatenate list with examples

Arvind is currently working with TSInfo Technologies as a Senior Python Developer. He is proficient in Python libraries like NumPy, and Tensorflow.