
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:
- Unveiling the Future of AI Detector
- CodeWithHarry Earns 20 Lakhs per month from YouTube?
- Cleaning Service Booking System in Python Tkinter
- Farmers Ecommerce App using Python Tkinter
- Guidelines for Project Collaboration Process
- The system of the binary conversion
- What is web development for beginners?
- Guide to Proxy Servers: How They Work and Why You Need Them?
- Python | Check Armstrong Number using for loop
- Python | Factorial of a number using for loop
- Link in bio
- Microsoft Giving Free Machine Learning Course: Enroll Now
- Accenture Giving Free Developer Certificate in 2023
- Python | Asking the user for input until they give a valid response
- Python | How to iterate through two lists in parallel?
- Amazon Summer Internship 2023
- Python | How to sort a dictionary by value?
- Amazon Giving Free Machine Learning Course with Certificate: Enroll Now
- Google Summer Internship 2023
- Python | How to split a list into equally-sized chunks?
- 5 Secret ChatGPT skills to make money
- Python | Remove items from a list while iterating
- Free Google Certification Courses
- 5 AI tools for coders better than ChatGPT
- Python | How to get dictionary keys as a list
- New secrets to Earn money with Python in 2023
- Flower classification using CNN
- How to represent Enum in Python?
- 5 methods | Flatten a list of lists | Convert nested list into a single list in Python
- What does if __name__ == __main__ do in Python?