Home >Backend Development >Python Tutorial >How to split three-digit numbers in Python

How to split three-digit numbers in Python

(*-*)浩
(*-*)浩Original
2019-07-04 14:05:2423205browse

How to split three-digit numbers in Python

Use mathematical methods to split (recommended learning: Python video tutorial)

The three-digit numbers are units, tens, and hundreds. They can be separated by dividing by 10.

n=123
while (n>0):
    l.append(n%10)
    n=n/10
l=list(reversed(l))#将顺序倒过来

At this time, split 123 into [1,2,3] and display it in a list

Of course, if it is python, there is a simpler way to convert the number into String

l=list(str(n))

Only one line is needed to achieve the result we want

For more Python-related technical articles, please visit the Python Tutorial column to learn !

The above is the detailed content of How to split three-digit numbers in Python. For more information, please follow other related articles on the PHP Chinese website!

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