- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove leading zeros from a Number given as a string using Python
In this article, we will learn a python program to remove leading zeros from a number given as a string.
Assume we have taken a number in string format. We will now remove all the leading zeros(zeros present at the beginning of a number) using the below-given methods.
Methods Used
The following are the various methods used to accomplish this task −
Using For Loop and remove() function
Using Regex
Using int() Function
Method 1: Using For Loop and remove() function
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task. −
Create a function deleteLeadingZeros() that removes the leading zeros from a number passed as a string to the function.
Use the for loop, to traverse through the length of the string using the len() function.
len() function − The number of items in an object is returned by the len() method. The len() function returns the number of characters in a string when the object is a string.
Use the if conditional statement along with != operator to check whether the current character in a string is not 0
Get the remaining characters of the string after the leading zeros using slicing.
Return the resultant string after removing all the leading 0s from the input string.
Return 0 if there are no leading 0s found.
Create a variable to store the input number passed as a string.
Call the above-defined deleteLeadingZeros() function by passing the input string to it to get the resultant string after removing leading zeros.
In the same way check for the other string having no leading zeros.
Example
The following program returns as a string that removes all the leading zeros from a number passed as a string using for Loop and remove() function −
# 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
On execution, the above program will generate 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)
Following are the Algorithms/steps to be followed to perform the desired task. −
Use the import keyword to import the regex(re) module.
Create a function deleteLeadingZeros() that removes the leading zeros from a number passed as a string to the function.
Create a variable to store the regex pattern for removing leading zeros from an input string.
Replace the matched regex pattern with an empty string using the sub() function.
sub() function(returns a string in which all the matching occurrences of the given pattern are replaced by the replace string).
Print the resultant string after removing all the leading 0s from the input string.
Example
The following program returns as a string that removes all the leading zeros from a number passed as a string using regular expressions −
# 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
On execution, the above program will generate the following output −
Given String is: 0002056 After Removing Leading Zeros: 2056
Method 3: Using int() Function
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task. −
Create a function deleteLeadingZeros() that removes the leading zeros from a number passed as a string to the function.
Use the int() function(returns an integer from a given object) to convert the input string to an integer. This function removes all the leading zeros.
Return the resultant number after removing all the leading 0s from the input string.
Example
The following program returns as a number that removes all the 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
On execution, the above program will generate the following output −
Given String is: 0002056 After Removing Leading Zeros: 2056
Conclusion
In this article, we learned how to remove leading zeros from a number given as a string using three different methods. We learned how to use slicing to get a subset of an iterable, such as a string, list, or tuple. We also learned how to utilize the regex module to replace (substitute) one pattern with another.