Reputation: 43
Hi I would like to split the following string "1234" in ['1', '2', '3', '4'] in python.
My current approach is using re module
import re
re.compile('(\d)').split("1234")
['', '1', '', '2', '', '3', '', '4', '']
But i get some extra empty strings. I am not an expert in regular expressions, what could be a proper regular expression in python to accomplish my task?
Please give me some advices.
Upvotes: 3
Views: 3811
Reputation: 122142
Strings are by default character lists:
>>> nums = "1234"
>>> for i in nums:
... print i
...
1
2
3
4
>>> nums[:-1]
'123'
Upvotes: 0
Reputation: 239523
Simply use list
function, like this
>>> list("1234")
['1', '2', '3', '4']
The list
function iterates the string, and creates a new list with all the characters in it.
Upvotes: 6