Python Remove Leading Zeros: 5 Easy Methods

Python Remove Leading Zeros: 5 Easy Methods

Sometimes, we want to remove leading zeros (0s) from a string like “00098” becomes “98” only. In Python, there are several ways to do this. We will be discussing some of them in this article.

"00921" --> "921"

1. The int() method to remove leading zeros in Python

You can use the Python int() method to remove leading zeros from a string like this-

my_string = "000213"
my_string = int(my_string)
print(my_string)

Output:

Output for the int() method to remove leading zeros in Python

The first method is the int() method, which converts a string into an integer. While converting, it will automatically remove leading zeros in the string. Note that the string should only contain numbers and no letters, alphabets, or other symbols.

2. The lstrip() method to remove leading zeros

The strip() method takes a string argument. This method will remove the string given as the argument from the string in operation. When used, it automatically removes leading zeros ( only ) from the string. Note that this works for numbers and all characters accepted as a string. However, another method strip() will remove the leading and ending characters from the string. Python lstrip() docs.

You can use the Python lstrip() method to remove leading zeros from a string like this-

my_string = "00913"
my_string = my_string.lstrip('0')
print(my_string)

Output:

Output for the lstrip() method to remove leading zeros
Output for the lstrip() method

3. Reassigning until the character in the 0th index is “0”:

In this method, we will loop through a string and check whether the string’s first character is “0”. If the first character is “0,” then we will reassign the string from the first index. This is fairly simple and can be understood by looking at an example.

You can use the reassigning method to remove leading zeros from a string like this-

my_string = '0002134'
for i in my_string:
    if i == '0':
	my_string = my_string[1:]		
print(my_string)

Output:

Output for the method of reassigning until the character in the 0th index is "0"

Output for the method of reassigning until the character in the 0th index is “0”

4. The startswith() method:

In this method, we will loop through a string and check whether the string’s first character is “0” with the startswith() method. This will return a boolean value, and we will use the same concept from 3rd way.

You can use the Python startswith() method to remove leading zeros from a string like this-

my_string = "00023"
for i in my_string:
    if i == "0":
	my_string = my_string[1:]	
print(my_string)
>> 23

my_string = "00023"
for i in my_string:
    if i == "0":
	my_string = my_string[1:]	
print(my_string)
>> 23

5. Using regex (Regular Expression) to remove leading zeros in Python string:

Python has a built-in Regular Expression module, namely, “re.” We can also remove the leading zeros from a string of numbers using this module. This is a simple task and can be understood once the code looks at.

The code:

import re
regex = "^0+(?!$)"
str = "000782"
str = re.sub(regex, "", str)
print(str)

Output:

Output for Using regex (Regular Expression) to remove leading zeros in Python string
Output for Using regex (Regular Expression) to remove leading zeros in Python string

Possible Reasons to remove leading zeros or trailing zeros from a number or a string?

  1. For formatting time – 01:24 to 1:24
  2. For stripping IP Address
  3. For stripping units of data: 4000 to 4K

Checkout Short Video:

Keep Learning, Keep Coding


Also Read:

Share:

Author: Ayush Purawr