Python Remove Leading Zeros: 5 Easy Methods

Python Remove Leading Zeros: 5 Easy Methods

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:

Output for the int() method to remove leading zeros in Python
Output for init() method

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:

Output for the lstrip() method to remove leading zeros
Output for the lstrip() method

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"
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:

Output for Using regex (Regular Expression) to remove leading zeros in Python string
Output for Using regex (Regular Expression) to remove leading zeros in Python string

Possible Reasons to remove leading zeros or trailing zeros from a number or a string?

  1. For formatting time – 01:24 to 1:24
  2. For stripping IP Address
  3. For stripping units of data: 4000 to 4K

Checkout Short Video:

Keep Learning, Keep Coding


Also Read:

  • I Love You Text in ASCII Art
    I Love You Text in ASCII Art This is simple I LOVE YOU Text in ASCII Art, you can use the button to copy this TEXT in ASCII Art and paste it into your system. Heart with Arrow Text in ASCII Art This is a simple Heart with Arrow Text in ASCII Art, you can…
  • Python Turtle Shapes- Square, Rectangle, Circle
    We all know that Python offers a variety of libraries for game Development, GUI Development, Data Visualization, and many more. These libraries not only make our work easy but also make the code efficient and deployable. Keeping this thing in mind, today, in this article, we will discuss and learn about Graphics Programming by using…
  • Python Turtle Commands and All Methods
    Introduction In this tutorial, we are going to learn about the python turtle commands and methods which we are using in the python turtle programming where Turtle is a python library that is used in creating games, pictures, and graphic images. If you have used Python Turtle a little bit, you may have observed that…
  • Happy Birthday Python Program In Turtle
    Introduction Hello and welcome to the copyassignment, today we will learn how to write the Happy Birthday Python Program In Turtle. Let’s say you want to wish Happy Birthday to someone with your Python skills, in that case, the code below can help you to wish Happy Birthday with Python Program In Turtle. Complete code for Happy…
  • I Love You Program In Python Turtle
    Introduction Hello and welcome to the copyassignment, today we will learn how to write the I Love You Program In Python Turtle. This could be very interesting for both beginners and experienced coders to learn. This could be simple and easy to understand because we have explained the code in simple terms. Click here if you want…
  • Draw Python Logo in Python Turtle
    Introduction Hello and welcome to the copyassignment, today we will learn how to Draw Python Logo in Python Turtle. This could be very interesting for both beginners and experienced coders to learn. This could be simple and easy to understand because we have explained the code in simple terms. Step 1: Importing Libraries Step 2: Creating a…
  • Space Invaders game using Python
    We have already developed multiple games using the turtle library and now continuing our series of projects development in Python using the turtle library, today, in this article, we are going to develop a Space Invaders game using Python.As we all know the number of various built-in functions that a turtle library offers is more…
  • How to make KBC Quiz game in python?
    Introduction Hi everyone, welcome to copyassignment.com. In this tutorial, we are going to learn how to make a KBC Quiz game in python using simple lines of code. We are going to use random and time modules. We are making the lines of code easy to understand as we provide a detailed explanation of the…
  • Draw Google Drive Logo Using Python
    Introduction: Hello and welcome to the copyassignment where we will learn how to Draw Google Drive Logo Using Python turtle. This could be very interesting for both beginners and experienced coders to learn. This could be simple and easy to understand because we have explained the code in simple terms. Well, Google Drive is a…
  • Draw Instagram Reel Logo Using Python
    Introduction Today’s topic will be how to Draw Instagram Reel Logo Using Python Turtle. Hey guys, tell us something that we all use in our daily lives. Someone wants to know how the Instagram reel logo was created. Can we create this logo in the simplest and most basic programming language, Python? Definitely! We can…

Share:
Avatar of Ankur Gajurel

Author: Ankur Gajurel

I am Ankur from Nepal trying to learn different aspects of Information Technology and Physics. I like building websites and minor projects with Python.