Remove Leading Zeros in Python String

  1. Use Iteration Statements to Remove Leading Zeros in a String in Python
  2. Use the lstrip() Function Along With list comprehension to Remove Leading Zeros in a String in Python
  3. Use the startswith() Method + Loop + List Slicing to Remove Leading Zeros in a String in Python

The removal of leading or trailing zeros is essential when data processing is performed, and the data has to be passed forward. A stray 0 might get attached to the string while it is transferred from one place to another, and it is recommended to remove it as it is unnecessary and inconvenient.

This tutorial demonstrates the several ways available to remove leading zeros in a string in Python.

Use Iteration Statements to Remove Leading Zeros in a String in Python

The simplest and most basic way to remove leading zeros in a string in Python is to remove them using iteration statements manually. Here, we manually create a code using a for loop along with the while loop to make a program that removes leading zeros in a string in Python.

The following code uses the for loop to remove leading zeros in a string in Python.

A = ['0001234','01000','1000',]
removeleading = []

for x in A:
  while x[0] == "0":
    x = x[1:]
  removeleading.append(x)
print(removeleading)

The above code provides the following output:

['1234', '1000', '1000']

In the above code, we use two iteration statements, a for loop and a while loop, the latter being nested within the former. Finally, we append the newly created list and then display it after making the necessary changes.

Use the lstrip() Function Along With list comprehension to Remove Leading Zeros in a String in Python

The lstrip() can be utilized to remove the leading characters of the string if they exist. By default, a space is the leading character to remove in the string.

List comprehension is a relatively shorter and very graceful way to create lists that are to be formed based on given values of an already existing list.

We can combine these two things and use them in our favor.

The following code uses the lstrip() function along with the list comprehension to remove leading zeros in a string in Python.

A = ['0001234','01000','1000',]
res = [ele.lstrip('0') for ele in A]
print (str(res))

The above code provides the following output.

['1234', '1000', '1000']

In the above code, the lstrip() function is used to strip the leading zeros in the given string. List comprehension is used here to extend the logic further and achieve this program successfully without any errors.

Use the startswith() Method + Loop + List Slicing to Remove Leading Zeros in a String in Python

The startswith() method provides a True value when the string starts with the value that the user in the function definition specifies. We combine this startswith() function with a loop and list slicing to remove leading zeros in a string in Python.

The following code uses the startswith() method + loop + list slicing to remove leading zeros in a string in Python.

A = ['0001234','01000','1000']
for idx in range(len(A)):
    if A[idx].startswith('0'):
        A[idx] = A[idx][1:]
print (str(A))

The above code provides the following output:

['001234', '1000', '1000']

In the above code, a for loop is opened for working, and the list slicing process is done with the help of the startswith() function.

The drawback with this method is that it only removes one leading zero at a run, which can be problematic with big numbers.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python String

  • Concatenate String and Int Values in Python
  • Raw String in Python