difference between string and list

L

lincoln rutledge

I'm having trouble figuring out the difference between a string and a
list.

I know that:
string = "foo bar"

is a list of characters, "foo bar", and string[0] is "f".

while:

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

strings have methods like string.count("f") returns 1. What methods do
lists have? Is it a similar class to string?

thanks,
Lincoln
 
P

Paul Rubin

lincoln rutledge said:
strings have methods like string.count("f") returns 1. What methods do
lists have? Is it a similar class to string?

Strings and lists are similar but not the same. dir(string) will show
you the methods available for strings. dir(list) will show you the
methods available for lists. See also the "built-in types" section
of the Python Library Reference Manual, the section on sequence types.
 
B

Bruno Desthuilliers

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
Is it a similar class to string?

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

"foo"[0] = "b"

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top