String question

Mark Tolonen M8R-yfto6h at mailinator.com
Mon Jun 23 23:38:51 EDT 2008


"Andreu" <root at sys1.org> wrote in message news:g3p72i$432$1 at aioe.org...
> Yes, ... don't ask me why, but in fact  v1,v2,v3 = str1.split()
> does not seem to work. My original problem was I forgot about
> the parenthesis as Tim point out. So I ended up converting to a
> list as in:  v = str1.split() and accessing the elements using
> v[0] v[1] ect...it is working now. Thanks.
>
> Andreu.

v1,v2,v3 = str1.split() will only work if there are exactly three things in 
str1.

>>> s = 'this is a test'
>>> v1,v2,v3 = s.split()
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ValueError: too many values to unpack
>>> v1,v2,v3 = s.split(' ',2)    # limit to two splits maximum
>>> v1
'this'
>>> v2
'is'
>>> v3
'a test'

-Mark




More information about the Python-list mailing list