[Tutor] how to split/partition a string on keywords?

eryksun eryksun at gmail.com
Thu Aug 23 23:17:07 CEST 2012


On Thu, Aug 23, 2012 at 4:03 PM, David Rock <david at graniteweb.com> wrote:
> text = raw_input("Enter text: ")
> sep = 'and'
> parts = text.split(sep)
> for i in parts[:-1]:
>     print i
>     print sep
> print [-1]

    >>> "band".split("and")
    ['b', '']

It needs to be sep = " and ". That's assuming we're ignoring tabs.
Line feed shouldn't be an issue since the source is raw_input.
Anything more advanced should probably use regular expressions.


More information about the Tutor mailing list