How to remove 0 from first number with python

王林
Release: 2024-02-22 14:46:03
forward
824 people have browsed it

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
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!