Iterate through Digits of a Number in Python : Various Methods

Iterate through Digits of a Number in Python

Python has many inbuilt functions that allow you to perform specific tasks on the input. If you want to iterate the digits of a number in python then the interpreter has functions for that. In this entire tutorial, you will know how to iterate through digits of a number in python using the various methods.

Problem Statement

Let’s say you have a number ‘1234567‘ and you want to iterate each digit in the number and print each of them on the screen. How you can do so?

In the next section, you will know all the methods to solve this problem.

 

Methods to iterate through digits of a number in python

Method 1: Iterate through digits of a number in python using the iter() function

The first method to iterate through digits of a number is the use of iter() function. It accepts the string value as the argument. Therefore you have to first typecast the integer value and then pass it into it.

Execute the below lines of code to iterate through digits.

integer = 1234567
for i in iter(str(integer)):
	print(i)

Output

1
2
3
4
5
6
7

Method 2: Iterate through digits using for loop

You can also iterate through each digit using the for loop only. The code will the same as the above method but here you will not use the iter() method.

Run the below lines of code.

num = 1234567

for digit in str(num):
	print(digit)

Output

1
2
3
4
5
6
7

Method 3: Iterate using the while loop

Here you will use the while loop to iterate the digits of the number. Inside the while loop, you will use the divmod() function. After that you will pass the input number and 10 to choose each digit in the loop.

You will get the below output when you will run the below lines of code.

num = 1234567

while num > 0:
	num, digit = divmod(num, 10)
	print(digit)

Output

7
6
5
4
3
2
1

to know more on the Python loop, please read the below article.

Loops in Python: Know the Full Tutorial with the Best Examples

Method 4: Using the map() function

You can also use the map() function to iterate through digits of a number. The map() function will accept the first parameter as ” int ” and the second parameter is the input number typecasted to string.

Run the below lines of code to iterate.

num = 1234
for c in map(int, str(num)):
	print(c)

Output

1
2
3
4
iterate through digits in python
iterate through digits in python

Conclusion

Iterating through digits of a number python is very useful when you want to manipulate each digit in a number like addition, multiplication, and comparing with other digits. The above methods are the way to iterate through digits of a number. I hope you have liked this tutorial. If you have any queries then you can contact us for help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner