breaking a string

Fredrik Lundh fredrik at pythonware.com
Wed Oct 17 09:06:20 CEST 2001


Michael Yuen wrote:
> I'm pretty new to this Python stuff and i'm trying to figure out how to
> break a string into it's component parts.
>
> For example, if a line of text in a file is:
> "Hello World!"
>
> I want to enter that into my list as just ['Hello', 'World']

>>> s = "Hello, World!"

>>> s.split()
['Hello,', 'World!']

>>> import re
>>> re.findall("\w+", s)
['Hello', 'World']

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list