I have been playing around with Python lately, and there is one operation that I have yet to figure out how to do. This is splitting a string into its individual characters, which I used to be able to do with other languages by simply splitting with an empty separator. In python, however, this returns an error.
How can I do this simple operation? Thanks...Code:>>> "testing123".split("") Traceback (most recent call last): File "<pyshell#14>", line 1, in ? "testing123".split("") ValueError: empty separator
I don't remember if there's such function (I'm a bit rusty), but it can be done in two lines, using a for-loop.
This shows how, using the interpreter:
Code:>>> the_string = "Hello, World!" >>> the_list = [] >>> for each_character in the_string: the_list.append(each_character) >>> the_list ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!'] >>>
Great, that works just fine. Thanks.
Try:
list("Any string")
- Paddy.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks