How to split integer into digits in python

Hi guys. How are you all? I hope you are all fine. Today in this tutorial we will learn about How to split integers into digits in python. There are so many methods that will be useful to convert integers into digits in python. So here we will learn about these methods. So without wasting your time let’s learn about this. I hope this will help you.

How to split integer into digits in python

  1. split integer into digits in python

    to split integer into digits in python just Use list comprehension. You can use list comprehension method to split the integer into digits in python. BY using this method you can easily convert the integer into digits. Here we will uise int() and str() function to convert int into digits. First of all take your integer number and then use these function. Lets learn this by given below example. I hope this will help you. mynum = 11011997 var1 = [int(a) for a in str(mynum)] print(var1) Output : [1, 1, 0, 1, 1, 9, 9, 7]

  2. How to split integer into digits in python

    to split integer into digits in python just Use math.ceil and math.log(). By using math.ceil and math.log() you can easily split the integer into digits in python. For this method first of all import math and then take your int number and use these functions and then print it. It will give you the digit. Thus you can change the integer into a digit in python. So let's learn about this by giving the below example. I hope this will help you. import math mynum = 11011997 var1 = [(mynum//(10**i))%10 for i in range(math.ceil(math.log(mynum, 10))-1, -1, -1)] print(var1) Output : [1, 1, 0, 1, 1, 9, 9, 7]

Method 1: Use list comprehension

You can use list comprehension method to split the integer into digits in python. BY using this method you can easily convert the integer into digits. Here we will uise int() and str() function to convert int into digits. First of all take your integer number and then use these function. Lets learn this by given below example. I hope this will help you.

mynum = 11011997
var1 = [int(a) for a in str(mynum)]
print(var1)

Output :

[1, 1, 0, 1, 1, 9, 9, 7]

Method 2: Use math.ceil and math.log()

By using math.ceil and math.log() you can easily split the integer into digits in python. For this method first of all import math and then take your int number and use these functions and then print it. It will give you the digit. Thus you can change the integer into a digit in python. So let’s learn about this by giving the below example. I hope this will help you.

import math
mynum = 11011997
var1 = [(mynum//(10**i))%10 for i in range(math.ceil(math.log(mynum, 10))-1, -1, -1)]
print(var1)

Output :

[1, 1, 0, 1, 1, 9, 9, 7]

Method 3: Use map() and str.split()

You can use map() and str.split() functions to split the integer into digit in python. For this method first of all take your string and use the split() fdunction with it and then use map() and then just print it. It will convert it from string to integer. So lets learn about of this by given below example.

str1 = "1 2 3 4 5 6"
list1 = str1.split()
var1 = map(int, list1)
result = list(var1)
print(result)

Output :

[1, 2, 3, 4, 5, 6]

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment