HOWTO · Python
How to Split an Integer Into Digits in Python
This tutorial demonstrates how to split an integer into digits in Python.
On this page
To split an integer into a list of its digits, convert it to a string and convert each character back to an integer with int(). This gives the digits from left to right. For a negative integer, treat the sign separately and split the absolute value; represent zero as [0]. Choose integer arithmetic when converting through a string is unsuitable, or use map() and str.split() when the input is already a whitespace-separated string of digit tokens.
Use List Comprehension to Split an Integer Into Digits in Python
A list comprehension is the most direct choice when the input is an integer and the desired result is a list of digits in their usual left-to-right order. str() exposes one character per decimal digit, and int() converts each character into an integer.
num = 13579
digits = [int(character) for character in str(num)]
print(digits)
The code produces the digits of 13579 as integer elements. For zero, the same expression returns [0]. For a negative input, split abs(num) and keep the sign in a separate variable if the application needs it. An integer does not retain leading zeros, so use the original text instead when those zeros matter.
Use Integer Arithmetic to Split an Integer Into Digits in Python
To avoid converting the value to text, repeatedly use divmod() with 10. Each remainder is the next digit from the right, so reverse the collected digits at the end. Handle zero explicitly because the loop otherwise has no digits to collect; use the absolute value and track the sign separately for negative inputs.
num = -13579
sign = -1 if num < 0 else 1
remaining = abs(num)
digits = []
if remaining == 0:
digits.append(0)
else:
while remaining:
remaining, digit = divmod(remaining, 10)
digits.append(digit)
digits.reverse()
print(sign, digits)
This arithmetic approach is useful when the conversion must stay numeric. The list is still ordered from most significant digit to least significant digit; the sign is separate from the digit values.
Use map() and str.split() to Convert Space-Separated Digits to Integers in Python
Use this method when a string already contains separate digit tokens, such as input read from a line. str.split() separates on whitespace by default, and map() applies int() to each token. This does not split an ordinary integer into its individual digits.
text = "1 3 5 7 9"
digits = list(map(int, text.split()))
print(digits)
This form is convenient for whitespace-separated values. If the source uses commas or another delimiter, pass that delimiter to split() and ensure each resulting token is a valid integer representation.
Use a for Loop to Split a Digit String Into Integers in Python
When the source is a digit string and an explicit loop is easier to adapt, slice one character at a time and convert each slice with int(). The step and slice width are both 1, so each iteration handles one decimal digit.
digit_text = "13579"
digits = []
for index in range(0, len(digit_text), 1):
digits.append(int(digit_text[index:index + 1]))
print(digits)
The loop produces the same left-to-right list as the list comprehension, while making the conversion step visible. Use the list comprehension for a concise integer-to-digits conversion, integer arithmetic when avoiding text conversion, map() with str.split() for already separated tokens, and a loop when you need to customize per-character processing. For text that may contain a sign or separators, handle those characters before converting digit slices.