Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Program for Remove leading zeros from a Number given as a string
Next article icon

Python Program for Remove leading zeros from a Number given as a string

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given numeric string str, the task is to remove all the leading zeros from a given string. If the string contains only zeros, then print a single "0".

Examples:

Input: str = "0001234" 
Output: 1234 
Explanation: 
Removal of leading substring "000" modifies the string to "1234". 
Hence, the final answer is "1234".

Input: str = "00000000" 
Output: 0 
Explanation: 
There are no numbers except 0

Remove leading zeros from a Number given

The simplest approach to solve the problem is to traverse the string up to the first non-zero character present in the string and store the remaining string starting from that index as the answer. If the entire string is traversed, it means all the characters in the string are '0'. For this case, store "0" as the answer. Print the final answer. 

Below is the implementation of the above idea:

Python
# Python Program to remove all the leading # zeros from a given numeric string  # Function to remove the leading zeros def removeLeadingZeros(num):      # traverse the entire string     for i in range(len(num)):          # check for the first non-zero character         if num[i] != '0':             # return the remaining string             res = num[i::];             return res;              # If the entire string is traversed     # that means it didn't have a single     # non-zero character, hence return "0"     return "0";  # Driver Code num = "1023"; print(removeLeadingZeros(num));  num = "00123"; print(removeLeadingZeros(num)); 

Output

1023
123

Time Complexity: O(N) 
Auxiliary Space: O(N)

Remove leading zeros from a Number given Using Space-Efficient Approach

Follow the steps below to solve the problem in constant space using Regular Expression: 

  • Create a Regular Expression as given below to remove the leading zeros

regex = "^0+(?!$)" 
where: 
^0+ match one or more zeros from the beginning of the string. 
(?!$) is a negative look-ahead expression, where "$" means the end of the string. 

  • Use the inbuilt replaceAll() method of the String class which accepts two parameters, a Regular Expression, and a Replacement String.
  • To remove the leading zeros, pass a Regex as the first parameter and empty string as the second parameter.
  • This method replaces the matched value with the given string.

Below is the implementation of the above approach:

Python
# Python3 Program to implement  # the above approach  import re  # Function to remove all leading  # zeros from a given string def removeLeadingZeros(str):      # Regex to remove leading      # zeros from a string      regex = "^0+(?!$)"      # Replaces the matched      # value with given string      str = re.sub(regex, "", str)      print(str)  # Driver Code  str = "0001234" removeLeadingZeros(str) 

Output

1234

Time Complexity: O(N), where N is the length of the string. 
Auxiliary Space: O(1)

Remove leading zeros from a Number given Using Built in methods

use the built-in string method lstrip to remove leading zeros. This method removes all leading characters that match a specified set of characters from a string. To remove leading zeros, we can pass '0' as the set of characters to remove.

Here is an example of how to use lstrip to remove leading zeros from a string:

Python
def remove_leading_zeros(num):     return num.lstrip('0')  print(remove_leading_zeros('0001234')) # Output: '1234' print(remove_leading_zeros('00000000')) # Output: '' 

Output
1234

 Time complexity:  O(N) 
 Auxiliary space: O(1), where N is the length of the string.
 

Please refer complete article on Remove leading zeros from a Number given as a string for more details!


Next Article
Python Program for Remove leading zeros from a Number given as a string

K

kartik
Improve
Article Tags :
  • Strings
  • Pattern Searching
  • Python
  • Python Programs
  • DSA
  • java-StringBuffer
  • regular-expression
Practice Tags :
  • Pattern Searching
  • python
  • Strings

Similar Reads

    Python program to remove last N characters from a string
    In this article, we’ll explore different ways to remove the last N characters from a string in Python. This common string manipulation task can be achieved using slicing, loops, or built-in methods for efficient and flexible solutions.Using String SlicingString slicing is one of the simplest and mos
    2 min read
    Python program for removing i-th character from a string
    In this article, we will explore different methods for removing the i-th character from a string in Python. The simplest method involves using string slicing.Using String SlicingString slicing allows us to create a substring by specifying the start and end index. Here, we use two slices to exclude t
    2 min read
    Python | Ways to remove numeric digits from given string
    In Python, we can remove numeric digits from a string in multiple ways. For example, if we have a string like "Geeks123" and we want to remove the numbers to get "Geeks", we can use different methods to accomplish this. We can do this by using techniques such as list comprehension, regular expressio
    3 min read
    Python - Remove leading 0 from Strings List
    Sometimes, while working with Python, we can have a problem in which we have data which we need to perform processing and then pass the data forward. One way to process is to remove a stray 0 that may get attached to a string while data transfer. Let's discuss certain ways in which this task can be
    5 min read
    Python | Remove all digits from a list of strings
    The problem is about removing all numeric digits from each string in a given list of strings. We are provided with a list where each element is a string and the task is to remove any digits (0-9) from each string, leaving only the non-digit characters. In this article, we'll explore multiple methods
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences