String question

cokofreedom at gmail.com cokofreedom at gmail.com
Tue Jun 24 09:53:21 CEST 2008


On Jun 24, 5:38 am, "Mark Tolonen" <M8R-yft... at mailinator.com> wrote:
> "Andreu" <r... at sys1.org> wrote in messagenews: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

In Python 3k I believe you can put a * next to one of the variables to
hold multiple arguments. That'll be aidful!



More information about the Python-list mailing list