HOWTO · Python
How to Remove Leading Zeros in Python String
This tutorial discusses the different ways to remove leading zeros in a Python string.
On this page
Python strings preserve their characters exactly, including leading zeros. To remove those zeros while keeping each value a string, call lstrip("0"). Convert to an integer only when the values are numeric and losing their original formatting is acceptable. The examples below show the direct method, its use with a list, and how to choose an alternative.
Remove leading zeros from one string with lstrip()
The lstrip() method returns a new string after removing characters from its left end. Passing "0" tells it to remove every consecutive zero character at the start; it does not remove zeros elsewhere in the string. The original value is unchanged because Python strings are immutable.
Run this example:
values = ["0001234", "01000", "1000"]
cleaned = [value.lstrip("0") for value in values]
print(cleaned)
['1234', '1000', '1000']
For instance, "0001200".lstrip("0") is "1200": the zeros after 12 remain. lstrip("0") removes the characters in the supplied set, not a fixed prefix substring, so use it when the goal is to remove any run of leading zero characters.
Apply the operation to a list of strings
The list comprehension above applies the same transformation independently to every value and preserves the list order. It also returns strings, which matters for identifiers such as postal codes, account numbers, and codes where formatting is meaningful. Assigning the result to a new variable keeps the input list available; to replace a list’s contents, assign the comprehension back to that variable.
Use value.lstrip("0") for each item when all the values are strings and only their leading zero characters should be removed. If values come from a file or another source, convert them to strings only if that matches the data model; converting arbitrary values can hide input problems.
Choose a method based on the data
For ordinary string cleanup, lstrip("0") is concise and preserves interior characters. A loop using startswith() and slicing is another option when the removal condition needs custom logic. The condition is safe for an empty string, and repeating it removes each leading zero while preserving interior zeros:
value = "0001200"
while value.startswith("0"):
value = value[1:]
print(value)
For this input, the loop leaves "1200". If the input contains only zeros, such as "000", it becomes an empty string, matching the rule that every leading zero is removed.
For numeric-only strings, str(int(value)) is a short conversion route. It changes the value into an integer and back, so it discards formatting such as plus signs and leading zeros, and it cannot preserve nonnumeric text. It is unsuitable for identifiers or strings that may not represent valid integers. A regular expression such as re.sub(r"^0+", "", value) can express the same leading-run match; it requires importing re and adds little for this simple case.
Handle empty strings and all-zero strings
Calling lstrip("0") on "" returns "". Calling it on "000" also returns "", because every character was removed. That is the natural result of removing all leading zeros, but some applications require an all-zero number to remain represented as "0". If so, handle that rule explicitly, for example by choosing value.lstrip("0") or "0" when the input is known to represent a number. Do not apply this fallback blindly to arbitrary text, because it changes the meaning of an originally blank value.
Test the result
Check representative inputs and the expected result for each: "0001234" becomes "1234", "01000" becomes "1000", and "1000" stays "1000". Also decide what your application expects for "" and "000" before processing real data. When preserving string semantics matters, verify that the transformed values remain strings and that interior zeros are still present. These checks help distinguish removing a prefix from converting the underlying value to a number.