String question

cokofreedom at cokofreedom at
Mon Jun 23 17:01:28 CEST 2008

On Jun 23, 4:45 pm, Andreu <r... at> 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

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

That seems to me to be the more pythonic way to do it, since it is

More information about the Python-list mailing list