How do i print an individual digit of a number in python?

i want to split up a raw_input into many integers like this

TheString = raw_input

>>>12345678

and then spit up TheString to integers

d1 = 1
d2 = 2
d3 = 3
d4 = 4
d5 = 5
d6 = 6
d7 = 7
d8 = 8

I tried to find it but it did not work what the others suggested.

Thank you in advance.

How do i print an individual digit of a number in python?

Jon Clements

134k31 gold badges240 silver badges273 bronze badges

asked Jan 10, 2014 at 12:18

0

Don't use different variable names for each digit. You can convert the string to a list of integers and index the list to get each digit.

>>> s = "314159" # or s = raw_input()
>>> d = map(int, s)
>>> d[0]
3
>>> d[1]
1

answered Jan 10, 2014 at 12:23

TimTim

11.2k4 gold badges40 silver badges43 bronze badges

I'm very new to python, any help is appreciated. Many thanks!

How do you write a function in Python that, given an int, prints each individual digit in the number on a new line, starting with the least significant digit (i.e. right to left)? And how do you then keep track of the number of digits and provide the number of digits as the return value of the function?

Map function executes a given function for each element of a given iterable such as tuple, list, etc.

Let understand with the following map function

mapObject = map(int, string_num)

Where

  • string_num is a list.
  • Int is function to which map passed each element of given list string_num
  • mapObject : Return Map object.

The following example illustrates map concepts to get desired Separate digits results.

giveNumber = 987654321

string_num = str(giveNumber)

mapObject = map(int, string_num)

separate_digit_list = list(mapObject)

print(separate_digit_list)

Result: [9, 8, 7, 6, 5, 4, 3, 2, 1]

Explanation:

  • First converted given number to str, str(giveNumber), for passing to map function as iterable. Required to convert str to support iteration.
  • Map function executes int function for each of string_num elements.
  • list(mapObject) Convert mapObject to the desired list of Separate numbers.

number = 987654321
for n in str(number):
 #print(type(n)) # Once enable will print 
 print(int(n)) # convert back to number from character

Result:

Explanation: We have given the number 987654321. For iterating numbers using a for loop, it requires converting into a string. If we display type on line 3, it will display for code print(type(n)).

Now some of us may raise concerns about what if we iterate numbers rather than convert them into strings?

We can try to attempt to execute the following code and play around with type iterations.

number = 987654321
for n in number:
 print(int(n))
 

Result: TypeError: int object is not iterable

It doesn’t allow to iterate numbers using a for loop in such a way and raise type error as int object is not iterable.

Therefore it requires converting from number to string before executing a loop as str(number). While iterating, each sequence represents a character and so it needs to convert each individual character to a number using int(n) to get the number sequence.

List comprehension gives powerful functionality in python to write code in a single line and achieve the desired result.

We have already gone through a code example demonstrating splitting numbers using a for a loop. Now we will rewrite the same program using List comprehension and understand techniques.

givenNumber = 987654321
charterList = [receivedDigit for receivedDigit in str(givenNumber)]
print(charterList)

Result:

['9', '8', '7', '6', '5', '4', '3', '2', '1']

Explanation:

  • Convert given number to string as str(givenNumber). Why? Explained here.
  • List comprehension breaks the given string (converted from number) into discrete digits as defined as receivedDigit in the program.
  • Collected list of digits (which is a list of characters) as charterList and print. /li>

Now we have received a list of individual characters rather than a list of individual numbers which was expected to be. Correct?

Just need to convert character to int within list comprehension. Here is the simple-to-read demonstration code

givenNumber = 987654321
charterList = [int(receivedDigit) for receivedDigit in str(givenNumber)]
print(charterList)

Here int(receivedDigit) convert each character over iteration and final output become a list of individual numbers as expected as below

[9, 8, 7, 6, 5, 4, 3, 2, 1]
  

In such a way we can split a two-digit number, three-digit number, four-digit number, and so on.

Following Code explains the list append function to collect each individual digit from a given number.

number = 987654321
result = []
for n in str(number):
	result.append(int(n))
print(result) 

Output: [9, 8, 7, 6, 5, 4, 3, 2, 1]

Explanation:

  • Create an empty list (result = []) before the loop starts.
  • Apply int function (int(n)) to convert each item from str to int value.
  • Append integer value return from int(n) to result list.

How do I get the individual digit of a number in Python?

4 Answers.
Use str to convert the number into a string so that you can iterate over it..
Use a list comprehension to split the string into individual digits..
Use int to convert the digits back into integers..

How do you find the individual digits of a number?

Steps:.
Declare a character array..
Assign number as string using sprintf() function in character array..
Extract the digits now by using simple loop from 0 to string length -1..
Print the each digit..

How do you split a number in a list Python?

To split a string into a list of integers:.
Use the str. split() method to split the string into a list of strings..
Use the map() function to convert each string into an integer..
Use the list() class to convert the map object to a list..