• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Remove Leading Zeros from String with lstrip() in Python

Remove Leading Zeros from String with lstrip() in Python

June 5, 2022 Leave a Comment

To remove leading zeros from a string in Python, the easiest way is to use the Python string lstrip() function.

str = "00001234"

print(str.lstrip("0"))

#Output:
1234

You can also use a while loop and slicing to remove leading zeros from a string.

str = "00001234"

while str[0] == "0":
    str[1:]

print(str)

#Output:
1234

When working with strings, the ability to easily be able to manipulate and change the values of those strings is valuable.

One such case is if you have leading zeros in a string which represents an ID or a record number and you want to get rid of those zeros.

To remove leading zeros from a string in Python, the easiest way is to use the Python string lstrip() function.

lstrip(), or “left strip”, removes a given character from the beginning of a string if they exist. By default, spaces are removed from the beginning of the string, but you can pass any character.

Below is a simple example showing you how to use lstrip() to remove leading zeros from a string in Python.

str = "00001234"

print(str.lstrip("0"))

#Output:
1234

Removing Leading Zeros from String in Python with While Loop and Slicing

Another way you can remove leading zeros from a string variable in your Python code is with a for loop and string slicing.

The idea here is that you loop until the first character is not a zero. If it is a zero, then you want to remove the first character from the string.

Below is an example of how to use a while loop and slicing to remove the leading zeros from a string in Python.

str = "00001234"

while str[0] == "0":
    str = str[1:]

print(str)

#Output:
1234

Removing Trailing Zeros from String in Python with rstrip() Function

If you have trailing zeros, instead of leading zeros, you can use the Python rstrip() function.

rstrip(), or “right strip”, removes a given character from the end of a string if they exist. By default, spaces are removed from the end of the string, but you can pass any character.

Below is an example of how you can remove trailing zeros from a string using rstrip().

str = "12340000"

print(str.rstrip("0"))

#Output:
1234

Removing Both Leading and Trailing Zeros in Python with strip() Function

If you have both leading and trailing zeros, you can use the Python strip() function.

strip() removes a given character from the beginning and end of a string if they exist. By default, spaces are removed from the beginning and end of the string, but you can pass any character.

Below is an example of how you can remove both leading and trailing zeros from a string using strip().

str = "000012340000"

print(str.strip("0"))

#Output:
1234

Hopefully this article has been useful for you to learn how to remove leading zeros from a string variable in Python.

Other Articles You'll Also Like:

  • 1.  Python Check if List Index Exists Using Python len() Function
  • 2.  pandas to_pickle – Write DataFrame to Pickle File
  • 3.  Remove Time from Datetime in Python
  • 4.  How to Get the Size of a List in Python Using len() Function
  • 5.  Remove Last Element from List Using Python
  • 6.  How to Check if a Letter is in a String Using Python
  • 7.  Create Symbolic Link with Python os.symlink() Function
  • 8.  Creating a List of Zeros in Python
  • 9.  Python max float – What’s the Maximum Float Value in Python?
  • 10.  Using Python to Check If a Number is a Perfect Square

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy