Remove leading zeros from a number from a string in Python

Removing Leading Zeros from a Number in Python

In this article, we will learn a Python program to remove leading zeros from a number given as a string.

Assume that we have a number in string format. Now we will use the method given below to remove all leading zeros (zeros at the beginning of the number).

Methods Used

The following are various methods used to accomplish this task

  • Using a For Loop and the remove() Function
  • Using Regex

  • Using the int() Function

Method 1: Using a For Loop and the remove() Function

Algorithm (Steps)

The following is the algorithm/steps to be followed to perform the required task. –

  • Create a function, deleteLeadingZeros(), to remove leading zeros from a number passed to it as a string.
  • Use a for loop to iterate over the length of the string using the len() function.

  • len() Function – The len() method returns the number of items in an object. When the object is a string, the len() function returns the number of characters in the string.

  • Use an if statement and the != operator to check if the current character in a string is zero.

  • Use a slice method to retrieve the remaining characters in a string after the leading zeros.

  • Return the resulting string after removing all leading zeros from the input string.

  • If no leading zeros are found, return 0.

  • Create a variable to store the input number passed as a string.

  • Call the deleteLeadingZeros() function defined above, passing it the input string, and return the resulting string with the leading zeros removed.

  • Use the same method to check other strings without leading zeros.

Example

The following program uses a for loop and the remove() function to remove all leading zeros from a number passed as a string and returns a string.

# creating a function that removes the leading zeros
# from a number passed as a string to the function
def deleteLeadingZeros(inputString):
   # traversing through the length of the string
   for k in range(len(inputString)):
      # checking whether the current character of a string is not 0
      if inputString[k] != '0':
         # getting the remaining string using slicing
         outputString= inputString[k::]
         # returning resultant string after removing leading 0s
         return outputString
   # returning 0 if there are no leading 0s found in the entire string
   return "0"
# input number as a string
inputString = "0002056"
print("Given String is:", inputString)
# calling the deleteLeadingZeros() function by passing the input string to it
print("After Removing Leading Zeros:", deleteLeadingZeros(inputString))
# checking it for other strings having no leading 0s
inputString = "256"
print("Given String is:", inputString)
# calling the deleteLeadingZeros() function by passing the input string to it
print("After Removing Leading Zeros:", deleteLeadingZeros(inputString))

Output

When executed, the above program will produce the following output:

Given String is: 0002056
After Removing Leading Zeros: 2056
Given String is: 256
After Removing Leading Zeros: 256

Method 2: Using Regex

Algorithm (Steps)

Below are the algorithm/steps to be followed to perform the required task. –

  • Use the import keyword to import the regex(re) module.
  • Create a function deleteLeadingZeros() to remove leading zeros from a number passed to the function as a string.

  • Create a variable to store the regex pattern used to remove leading zeros from the input string.

  • Use the sub() function to replace the matched regex pattern with an empty string.

  • sub() function (returns a string where all occurrences of the given pattern are replaced with the replacement string).

  • Prints the resulting string after removing all leading zeros from the input string.

Example

The following program returns a string that removes all leading zeros from a number passed using the regular expression -.

# importing re module
import re
# creating a function that removes the leading zeros
# from a number passed as a string to the function
def deleteLeadingZeros(inputString):
   # regex pattern for removing leading zeros from an input string
   regexPattern = "^0+(?!$)"
   # Replace the matched regex pattern with an empty string
   outputString = re.sub(regexPattern, "", inputString)
   # returning output string after removing leading 0s
   return outputString
# input number as a string
inputString = "0002056"
print("Given String is:", inputString)
# calling the deleteLeadingZeros() function by passing the input string to it
print("After Removing Leading Zeros:", deleteLeadingZeros(inputString))

Output

When executed, the above program will produce the following output:

Given String is: 0002056
After Removing Leading Zeros: 2056

Method 3: Using the int() Function

Algorithm (Steps)

Below are the algorithm/steps to be followed to perform the required task. –

  • Create a function, deleteLeadingZeros(), to remove leading zeros from a number passed to it as a string.
  • Use the int() function (which returns an integer from a given object) to convert the input string into an integer. This function removes all leading zeros.

  • Returns the number after removing all leading zeros from the input string.

Example

The following program returns a number by removing all leading zeros – from a number passed as a string using the int() function.

# creating a function that removes the leading zeros
# from a number passed as a string to the function
def deleteLeadingZeros(inputString):
   # converting the input string to an integer removes all the leading zeros
   result = int(inputString)
   # returning the resultant number after removing leading zeros
   return result
# input number as a string
inputString = "0002056"
print("Given String is:", inputString)
# calling the deleteLeadingZeros() function by passing the input string to it
print("After Removing Leading Zeros:", deleteLeadingZeros(inputString))

Output

When executed, the above program will produce the following output:

Given String is: 0002056
After Removing Leading Zeros: 2056

Summary

In this post, we learned how to remove leading zeros from a number given as a string using three different methods. We also learned how to use slices to obtain a subset of an iterable, such as a string, list, or tuple. We also learned how to use the regex module to replace (substitute) one pattern with another.

Leave a Reply

Your email address will not be published. Required fields are marked *