
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:

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:

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”
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:

Possible Reasons to remove leading zeros or trailing zeros from a number or a string?
- For formatting time – 01:24 to 1:24
- For stripping IP Address
- For stripping units of data: 4000 to 4K
Checkout Short Video:
Keep Learning, Keep Coding
Also Read:
- Top 25 Pattern Programs in C++
- Currency Converter in C++
- SQLite | CRUD Operations in Python
- Number Guessing Game in C++
- Image background remover in Python
- C++ Project Structure
- Python | Check if a string is a palindrome or not without Recursion
- Python | Check if a number is an Armstrong Number or not using Recursion
- Python | Check if a number is an Armstrong Number or not without using Recursion
- Python | Shuffle a list using recursion
- Python | Shuffle a list without recursion
- Python | Implementing switch case using functions
- Python function to check whether a number is perfect or not
- Python | Find LCM using function
- Python | Find HCF using function
- Python | Convert the binary number to decimal without using library function
- Python | Create a basic operations calculator(+, -, /, and x), create a separate function for each operation
- Python | Detecting the number of local variables declared in a function
- Python | Making a chain of function decorators (bold italic underline etc)
- Python | Access function inside a function
- Event Management System Project in Python
- ATM machine program in C++
- Python | Create a function with a pass statement
- Python | Function to calculate the square root of a number
- Python | A function that calculates the power of a number
- Python | A function that accepts 2 integers and adds them and returns their sum
- Python | Function that takes a list of integers and returns the last integer
- Python | Return multiple values from a function
- Python function that takes a list and returns a new list with unique elements of the first list
- Python | Generate a random number in the range of 1 to 10 using the function