If you want to "parse" a number the easiest way to do this is to convert it to string. You can convert an int to string like this s = string(500)
. Then use string index to get character that you want. For example if you want first character (number) then use this string_name[0]
, for second character (number) use string_name[1]
. To get length of your string (number) use len(string)
. And to check if number is odd or even mod it with 2.
# Converting int to string
int_to_sting = str(97723)
# Getting number of characters in your string (in this case number)
n_of_numbers = len(int_to_sting)
# Example usage of string index
print("First number in your number is: ",int_to_sting[0])
print("Second number in your number is: ",int_to_sting[1])
# We need to check for every number, and since the first number is int_to_sting[0] and len(int_to_sting) returns actual length of string we need to reduce it by 1
for i in range(n_of_numbers-1):
if int_to_sting[i]%2==0:
print(int_to_sting[i]," is even")
else:
print(int_to_sting[i]," is odd")