2

If you write like

n = str(input())

n = n.split()

print(n)

That will work. But if you try to do it with integers, you will get

`Value Error`.

How to do it with int type?

4
  • Please format as code Commented Mar 2, 2018 at 7:53
  • Show the error. I'm unable to reproduce your problem. Commented Mar 2, 2018 at 7:54
  • Pleas show your simple code. Commented Mar 2, 2018 at 8:18
  • to split a string with no delimiter ( like space or comma), have a look at : this question ...but, an error cannot be the result of the above code unless the result of input() is not simply an integer. Commented Mar 2, 2018 at 8:25

3 Answers 3

4

Do you want to separate several numbers? 1 2 3 -> [1, 2, 3]

n = str(input())
n = n.split()
numbers = [int(i) for i in n]
print(numbers)

Or split a number in numeral? 123 -> [1, 2, 3]

n = str(input())
numbers = [int(i) for i in n]
print(numbers)

Use Nikhil answer, if you want to split a number with delimiters 1%3 -> [1, 3]

Sign up to request clarification or add additional context in comments.

Comments

2

You can split integer value with following ways..

  1. list comprehension

    n = str(input())
    result = [x for x in n]
    print(result)
    
  1. using list object

     n = str(input())
     result = [x for x in n]
     print(result)
    
  2. using map object

     n = str(input())
     result = list(map(int,n))
     print(result)
    

Comments

0

You can do that like this,

n = 567
a = str(n).split(YOUR DELIMITER)

Like if YOUR DELIMITER = 6, Then if i print(a) then i get,

['5', '7']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.