Create list from string
Peter Otten
__peter__ at web.de
Fri Jun 13 10:28:21 EDT 2008
ericdaniel wrote:
> I'm new to Python and I need to do the following:
>
> from this: s = "978654321"
> to this : ["978", "654", "321"]
Use a loop:
>>> s = "978654321"
>>> items = []
>>> for start in range(0, len(s), 3):
... items.append(s[start:start+3])
...
>>> items
['978', '654', '321']
or a list comprehension:
>>> [s[start:start+3] for start in range(0, len(s), 3)]
['978', '654', '321']
Peter
More information about the Python-list
mailing list