difference between string and list

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Feb 27 16:10:48 EST 2007


lincoln rutledge a écrit :
> I'm having trouble figuring out the difference between a string and a
> list.

['R', 'e', 'a', 'l', 'l', 'y', ' ', '?', ' ', 'S', 'e', 'e', 'm', 's', ' 
', 'q', 'u', 'i', 't', 'e', ' ', 'o', 'b', 'v', 'i', 'o', 'u', 's', ' ', 
't', 'o', ' ', 'm', 'e', '.']


> I know that:
> string = "foo bar"
> 
> is a list of characters

No. It's a string. Python doesn't have a "character" type.

>, "foo bar", and string[0] is "f".

Yes. And ?

> while:
> 
> list = ["foo", "bar"]
> and list[0] is "foo".

So ?

> strings have methods like string.count("f") returns 1.  What methods do
> lists have?

Open your interactive Python interpreter and type
 >>> help(list)


>  Is it a similar class to string?

Not exactly. Both are sequences, that's all. FWIW, try this:

"foo"[0] = "b"

HTH



More information about the Python-list mailing list