[Tutor] Split Method

Steven D'Aprano steve at pearwood.info
Thu Jan 30 12:22:21 CET 2014


On Thu, Jan 30, 2014 at 12:11:56PM +0100, Rafael Knuth wrote:
> Hey there,
> 
> I am having some issues with splitting strings.
> I already know how to split strings that are separated through empty spaces:
> 
> def SplitMyStrings():
>     Colors = "red blue green white black".split()
>     return (Colors)
> 
> print(SplitMyStrings())
> 
> >>>
> ['red', 'blue', 'green', 'white', 'black']
> 
> ... however I couldn't figure out how to split each character into a list item.

list(some_string)

For example:

py> list("hello world")
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']


Note that the space is considered an ordinary character. If you want to 
ignore spaces:

py> list("hello world".replace(" ", ""))
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']


-- 
Steven


More information about the Tutor mailing list