12

I take a string of integers as input and there are no spaces or any kind of separator:

12345

Now I want this string to converted into a list of individual digits

[1,2,3,4,5]

I've tried both

numlist = map(int,input().split(""))

and

numlist = map(int,input().split(""))

Both of them give me Empty Separator error. Is there any other function to perform this task?

0

4 Answers 4

21

You don't need to use split here:

>>> a = "12345"    
>>> map(int, a)
[1, 2, 3, 4, 5]

Strings are Iterable too

For python 3x:

list(map(int, a))
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I was looing for. This perfectly served my purpose. My question is marked as duplicate, but in the other question, no answer explains how to split an input as in this answer. Please see to that @Carsten.
5

Use list_comprehension.

>>> s = "12345"
>>> [int(i) for i in s]
[1, 2, 3, 4, 5]

1 Comment

Love this. So expressive.
1

The answers are brilliant. Here is an approach using regex

>>> import re
>>> s = '12345'
>>> re.findall(r'\d',s)
['1', '2', '3', '4', '5']

Ref - Docs on Regex

Why use regex for everything?

Comments

-3

You also can use

>>> range(1,6)
[1, 2, 3, 4, 5]

4 Comments

That doesn't convert from a string though...
Where it is taking the string...
I want to take an input
Oh, yes. you right, but if so it can be used as work around

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.