Remove leading and trailing Zeros from a string in Python

avatar

Borislav Hadzhiev

Last updated: Jul 10, 2022

banner

Photo from Unsplash

Remove leading and trailing Zeros from a string in Python #

Use the str.strip() method to remove the leading and trailing zeros from a string, e.g. result = my_str.strip('0'). The str.strip() method will return a copy of the string with the leading and trailing zeros removed.

main.py
my_str = '00052140000' # ✅ remove leading and trailing zeros from string result = my_str.strip('0') print(result) # 👉️ '5214' # --------------------------------------- # ✅ remove leading and trailing zeros from list of strings my_list = ['00120', '003400', '005600'] result = [item.strip('0') for item in my_list] print(result) # 👉️ ['12', '34', '56']

We used the str.strip() method to remove the leading and trailing zeros from a string.

Note that there are also str.lstrip() and str.rstrip() methods to remove only the leading or trailing zeros.
main.py
print('005400'.strip('0')) # 👉️ '54' print('005400'.lstrip('0')) # 👉️ '5400' print('005400'.rstrip('0')) # 👉️ '0054'

The str.strip method returns a copy of the string with the provided leading and trailing characters removed.

The method does not change the original string, it returns a new string. Strings are immutable in Python.

The str.strip() removes the provided characters from the leading and trailing ends until it reaches a character that is not contained in the provided set.

You might also have to handle the scenario where your string contains a single 0.

main.py
print('0'.strip('0')) # 👉️ "" # 👇️ return '0' if string only contains zeros print('0'.strip('0') or '0') # 👉️ "0"

You can use the or operator to provide a fallback value if the string only contains zeros.

If stripping the zeros from the string returns an empty string (a falsy value), we return a string containing a zero.

Remove leading and trailing Zeros from a list of strings #

To remove leading and trailing Zeros from a list of strings:

  1. Use a list comprehension to iterate over the list.
  2. On each iteration, use the str.strip() method to remove the leading and trailing zeros.
  3. The strings in the new list won't contain any leading or trailing zeros.
main.py
my_str = '00052140000' my_list = ['00120', '003400', '005600'] result = [item.strip('0') for item in my_list] print(result) # 👉️ ['12', '34', '56']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we call the strip() method on the string to remove any leading and trailing zeros and return the result.

If you need to remove only the leading zeros from each string in the list, use the str.lstrip() method.

main.py
my_list = ['00120', '003400', '005600'] result = [item.lstrip('0') for item in my_list] print(result) # 👉️ ['120', '3400', '5600']

Similarly, if you only want to remove the trailing zeros from each string in the list, use the str.rstrip() method.

main.py
my_list = ['00120', '003400', '005600'] result = [item.rstrip('0') for item in my_list] print(result) # 👉️ ['0012', '0034', '0056']

The str.lstrip method returns a copy of the string with the leading characters removed.

The str.rstrip method returns a copy of the string with the trailing characters removed.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.