While learning Python I've come across this several times, but I've never been able to get it to work, so I always work around it. No more I say! I'm using 3.1, so I'm assuming that string.split doesn't work in 3.0 and later.

Say I want to split "Why does string.split() not work?" into ["Why", "does", "string.split()", "not", "work?"].

I'm told to use:

a = "Why does string.split() not work?"
string.split(a)
print("SUCCESS!", a)

NameError: name 'string' is not defined

No dice. So what do I use instead of the string.split() that these old tutorials constantly advocate?

You are using old style Python code! Long ago it used to be like this ...

import string

a = "Why does string.split() not work?"
x = string.split(a)
print("SUCCESS!", x)

... now it's like this ...

a = "Why does string.split() not work?"
x = a.split()
print("SUCCESS!", x)

Edited 7 Years Ago by vegaseat: n/a

Brilliant! Thanks. This has been bugging me for a long time. Glad I finally cracked and asked for help :icon_smile:

This question has already been answered. Start a new discussion instead.