Python Forum

Full Version: "Slicing" number
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

what is the best way to "slice" a number?
I mean, I want to get all the digits of a number.

I want something like this:

num = 123456

strnum = str(num)
for i in range (len(strnum)+1):
  digit = strnum[i]
Thank
num = 1234554321
digits = list(str(num))
print(digits)
# or using list comprehension
digits2 = [n for n in str(num)]
print(digits2)
Thank you!