difference between string and list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lincoln rutledge

    #1

    difference between string and list

    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

  • Paul Rubin

    #2
    Re: difference between string and list

    "lincoln rutledge" <lincolnr@oar.n etwrites:
    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.

    Comment

    • Bruno Desthuilliers

      #3
      Re: difference between string and list

      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

      Comment

      Working...