How to get the separate digits of an int number?
To get the separate digits of an integer number, you can convert the number to a string, and then use the list()
function to convert the string to a list of characters.
Here is an example:
# Get the separate digits of the number 12345
number = 12345
# Convert the number to a string
number_str = str(number)
# Convert the string to a list of characters
digits = list(number_str)
# Print the list of digits
print(digits)
The output will be: ['1', '2', '3', '4', '5']
You can also use a loop to iterate over the digits and convert them back to integers if needed:
# Get the separate digits of the number 12345
number = 12345
# Convert the number to a string
number_str = str(number)
# Iterate over the characters in the string and convert them to integers
for digit in number_str:
digit = int(digit)
# Do something with the digit
print(digit)
The output will be: 1 2 3 4 5