简体   繁体   中英

How can I separate a double digit to two single digits with python? e.g. 15 -> 1, 5

def plus_cycles():
  n = int(input("Write here the number that you want. (1~99)\n"))
  while True:
    if n < 10:
      a = "n" + "0"

In this code, I want a to be separated with two single digits. eg 15 -> 1, 5 or [1, 5]

The divmod() method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder. For integers, the result is the same as (a // b, a % b) .

>>>> divmod(15, 10)
(1, 5)
>>> 15 // 10
1
>>> 15 % 10
5

You Could do something like this:

>>> intToList = lambda number : [int(i) for i in list(str(number))]
>>> intToList(15)
[1, 5]

list(str(15)) Will return a List of Chars like:

>>> list(str(15))
['1', '5']

So we iterate through them with a for loop to turn them back ino integers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM