String question
cokofreedom at gmail.com
cokofreedom at gmail.com
Mon Jun 23 11:01:28 EDT 2008
On Jun 23, 4:45 pm, Andreu <r... at sys1.org> 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.
More information about the Python-list
mailing list