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)

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

Good! Just a note, Python3 no longer allows the old style coding!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.