Question

I'm trying some text manipulation stuff out and am bumping into the error:

wordlist = string.split(" ")
AttributeError: 'module' object has no attribute 'split'

I have imported the string module like so:

import string

When I list the methods available in the string module, split() is not there as expected.

['ChainMap', 'Formatter', 'Template', '_TemplateMetaclass', '__builtins__', 
'__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', 
'__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase',
'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 
'whitespace']

I'm not sure if this is an error on my end, or if there is a new method for split. How do I split my strings via the whitespace between them?

Was it helpful?

Solution

You dont have to import the string module for splitting. You can simply run a split on your string variable

str = "testing 1 2 3"
print str.split(" ") #or str.split()

output

['testing', '1', '2', '3']

OTHER TIPS

string.split is deprecated in 2.x and gone in 3.x.

Just do:

yourStringName.split(' ')

With reference to string.split error? python

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top