>386489 or there is an integer starting with 0"/> >386489 or there is an integer starting with 0">

Home  >  Article  >  Backend Development  >  How to remove 0 from first number with python

How to remove 0 from first number with python

王林
王林forward
2024-02-22 14:46:03833browse

How to remove 0 from first number with python

Question content

Even though the type of the variable is char, I need to remove all 0s at the beginning of numbre:

0
Number = "0000386489"

"0000386489"  --->>  386489

Or there is an integer starting with 0


Correct answer


You can accomplish this task in a number of ways.

  1. Use int and str conversion operations:

    txt_number = "00036"; txt_number = str(int(txt_number))

However, this only works with integers and may not be the best approach for floating point numbers.

  1. Another method is to use the lstrip operation directly on the string.

    txt_number = "0036".lstrip("0")

The above is the detailed content of How to remove 0 from first number with python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete