What's the difference between str and 'hello' ?

paulC pclinch at gmail.com
Thu May 19 06:18:30 EDT 2005


hong Yu wrote:
> I am new to python.
> I was confused:
>
> >>> a = list('hello')
>
> >>> a
> ['h', 'e', 'l', 'l', 'o']
>
> I want to combine the items in the list into a string.
> So:
>
> >>> ''.join(a)
> 'hello'
>
> but:
>
> >>> str.join(a)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: descriptor 'join' requires a 'str' object but received a
'list'
>
> So why TypeError raised?
> and
> What's the difference between str and 'hello' ?
>
> Thanks a lot.


str is a class, 'hello' is instance of the class; the method join
expects to receive an instance as its first parameter so:-

>>> str.join( '', a )
'hello'

Regards, Paul




More information about the Python-list mailing list