What about a basic

your_string.strip("0")

to remove both trailing and leading zeros ? If you're only interested in removing trailing zeros, use .rstrip instead (and .lstrip for only the leading ones).

More info in the doc.

You could use some list comprehension to get the sequences you want like so:

trailing_removed = [s.rstrip("0") for s in listOfNum]
leading_removed = [s.lstrip("0") for s in listOfNum]
both_removed = [s.strip("0") for s in listOfNum]
Answer from Pierre GM on Stack Overflow
🌐
Kodeclik
kodeclik.com › remove-leading-zeros-in-python-string
Remove Leading Zeros from a given Python String
September 19, 2024 - Five ways to remove leading zeros from a Python string. 1. Use a for loop. 2. Use string slicing operators in a while loop. 3. Use the lstrip() method. 4. Recursive removal of leading zeros. 5. Use int() and str() casting functions.
Discussions

How can I get the "format" function to leave my leading ZEROs in place?
I have some binary data that I want to hash. Specifically I am hashing a 128 bit private key. But for the discussion let’s just use a 16 bit private key. So here is my code and it works great except when my private key begins with leading zero’s. Specifically 4 leading zeros or more. More on discuss.python.org
🌐 discuss.python.org
4
0
September 23, 2023
Remove the leading zero before a number in python - Stack Overflow
I have a string which correspond to a number but with several leading zeros before it. What I want to do is to remove the zeros that are before the number but not in the number. For example "000000... More on stackoverflow.com
🌐 stackoverflow.com
How do I write a Regex in Python to remove leading zeros for a number in the middle of a string - Stack Overflow
I have a string composed of both letters followed by a number, and I need to remove all the letters, as well as the leading zeros in the number. For example: in the test string U012034, I want to m... More on stackoverflow.com
🌐 stackoverflow.com
python 3.x - How can I format a string to remove leading zeros? - Stack Overflow
Suppose I have a number that I want to put in a string. ... Sign up to request clarification or add additional context in comments. ... Suppose x was a float and I wanted to truncate it to two decimal places but remove the leading zero. Is there an easy way to do that in python 3? More on stackoverflow.com
🌐 stackoverflow.com
July 11, 2017
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to remove leading zeros from a number given as a string in python
5 Best Ways to Remove Leading Zeros from a Number Given as a String in Python - Be on the Right Side of Change
February 26, 2024 - The regular expression pattern r'^0+' matches one or more zeros at the beginning of the string, and '' indicates that this matched part should be replaced with an empty string, effectively removing all leading zeros.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-leading-0-from-strings-list
Python - Remove leading 0 from Strings List - GeeksforGeeks
April 4, 2023 - Print the original list of strings. Use a list comprehension to loop through each element in the list. For each element, use the lstrip() method to remove any leading 0s.
🌐
Python.org
discuss.python.org › python help
How can I get the "format" function to leave my leading ZEROs in place? - Python Help - Discussions on Python.org
September 23, 2023 - I have some binary data that I want to hash. Specifically I am hashing a 128 bit private key. But for the discussion let’s just use a 16 bit private key. So here is my code and it works great except when my private key begins with leading zero’s. Specifically 4 leading zeros or more.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-for-remove-leading-zeros-from-a-number-given-as-a-string
Python Program for Remove leading zeros from a Number given as a string - GeeksforGeeks
July 23, 2025 - 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: ... # Python3 Program ...
🌐
Delft Stack
delftstack.com › home › howto › python › python remove leading zeros
How to Remove Leading Zeros in Python String | Delft Stack
February 2, 2024 - The tutorial explores various methods to remove leading zeros in Python, presenting iterative approaches using loops, the lstrip() function combined with list comprehension, and leveraging the int() and str() functions for conversion.
Find elsewhere
🌐
Medium
medium.com › vicipyweb3 › mastering-leading-zero-magic-a-friendly-guide-to-formatting-leading-zeros-in-python-3cb934d7aa4f
Mastering Leading Zero Magic: A Friendly Guide to Formatting Leading Zeros in Python | by PythonistaSage | ViciPyWeb3 | Medium
May 3, 2023 - The f-string formats it with a minimum width of 2 characters, padding with a leading zero to get the result '07'. If you have a number with more digits than the specified width, it won’t be truncated or altered. For example: number = 123 formatted_number = f"{number:02}" print(formatted_number) # Output: '123' In this case, the output is '123', as the number already has more digits than the specified width. F-strings are a powerful Python feature that can help you format strings with ease.
🌐
TutorialsPoint
tutorialspoint.com › article › remove-leading-zeros-from-a-number-given-as-a-string-using-python
Remove leading zeros from a Number given as a string using Python
March 27, 2026 - We use the pattern ^0+(?!$) to match leading zeros but preserve a single zero if the entire string is zeros ? import re def remove_leading_zeros_regex(input_string): # Pattern: ^0+ matches one or more zeros at start # (?!$) negative lookahead ensures we don't match if string is only zeros pattern = "^0+(?!$)" result = re.sub(pattern, "", input_string) return result # Test with different strings test_strings = ["0002056", "00000", "256", "0050"] for string in test_strings: result = remove_leading_zeros_regex(string) print(f"'{string}' ?
🌐
Reddit
reddit.com › r/learnpython › how does split () remove leading zeros when used in a loop?
r/learnpython on Reddit: How does split () remove leading zeros when used in a loop?
March 18, 2021 -

My motivation here is to parse a string and do some calculations with the time of day...for instance 6:05 . I could just use split() and have hour="6" and minute ="05"but if I understand correctly, leading zeros won't work for calculations. I could use a method to convert to an integer to do it but saw that some people just loop through like this:

txt = "6:05"
x, y= (int(i) for i in txt.split(":"))
print(x)
print(y)
#answer:
#6
#5

So what is happening here exactly? I'm guessing the int(i) converts the string "05" into the integer 5 somehow but can't find the syntax for that when i've been searching for python loops and splits. If someone could explain this, I would appreciate it.

*edit*, thanks for the help guys!

🌐
Kodeclik
kodeclik.com › remove-trailing-zeros-in-python-string
How to remove Trailing Zeros from a given Python String
July 15, 2025 - Five ways to remove trailing zeros from a Python string. 1. Use a for loop. 2. Use string slicing operators in a while loop. 3. Use the rstrip() method. 4. Recursive removal of trailing zeros. 5. Use float() and str() functions.
🌐
w3resource
w3resource.com › python-exercises › string › python-data-type-string-exercise-63.php
Python: Remove leading zeros from an IP address - w3resource
June 12, 2025 - # Define a function that takes an IP address as input and removes leading zeros from each octet def remove_zeros_from_ip(ip_add): # Split the IP address into its octets and convert each octet to an integer with no leading zeros new_ip_add = ".".join([str(int(i)) for i in ip_add.split(".")]) # Return the new IP address with no leading zeros return new_ip_add ; # Test the function with two example IP addresses print(remove_zeros_from_ip("255.024.01.01")) print(remove_zeros_from_ip("127.0.0.01 ")) ... Write a Python program to strip leading zeros from each segment of an IP address using split and int conversion.
🌐
Coderwall
coderwall.com › p › drqn9g › removing-leading-zeroes-in-python-s-strftime
Removing leading zeroes in Python's strftime (Example)
February 25, 2016 - Update: See the comment below from aloschilov for the best way to removing leading zeroes, which is to use a - before the formatter (after the %), e.g.: %-H. I was unaware of this syntax when I wrote this tip.
🌐
Blogger
linux-hints.blogspot.com › 2010 › 08 › python-remove-leading-zeros.html
Linux hints: Python - remove leading zeros
August 8, 2010 - To remove one or more leading zeros from the string eg. convert 0040 into 40: import re oldstring = '0040' newstring = re.sub(r"\b0{2}","",oldstring) To remove different number of zeros change the number in {} in the code.