On Jun 23, 4:45 pm, Andreu <[EMAIL PROTECTED]> wrote:
> I want to split a sentence and assign each word to a variable.
> In Ruby I can do it as:
>
> v1,v2,v3,v4,v5 = str1.split
>
> Which will be the Python equivalent ? Thanks.
>
> Andrew.

Well a straight copy would be...

>>> example = "Hello, how are you"
>>> v1, v2, v3, v4 = example.split()
>>> print v1, v2, v3, v4
Hello, how are you
>>> print v1
Hello,

However I would make a list of it.

>>> v_list = example.split()
>>> print v_list
['Hello,', 'how', 'are', 'you']
>>> for word in v_list:
        print word
Hello,
how
are
you

That seems to me to be the more pythonic way to do it, since it is
dynamic.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to