Difference between Strings and Lists in Python

Discussion in 'Python' started by lionaneesh, Mar 7, 2012.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Strings and Lists are 2 important data types in the suite of structured data types in python. Strings and lists are similar, but they are not same and many people don’t know the main difference between a string and a list in python. One simple difference between strings and lists is that lists can any type of data i.e. integers, characters, strings etc, while strings can only hold a set of characters.

    Let’s try the following set of Code:-

    Code:
    s = [‘H’,’e’,’l’,’l’,’o’]
    s[0] = 'Y'
    print s 
    
    Output:-

    Code:
    ['Y','e','l','l','o']
    
    As expected it works fine.

    Now let’s try the same with strings:-

    Code:
    s = 'Hello'
    s[0] = 'Y'
    
    print s
    
    Output:-

    Code:
    s[0] = 'Y'
    TypeError: 'str' object does not support item assignment
    
    Another Main difference between stings and lists which we’ll discuss in this tutorial is Mutation.

    Mutation



    Though it sounds scary, mutation is actually a very simple concept. What Mutation means is that we can change the value of a list after we have created it, but we cannot in case of strings, one might question that we can change a value of a string by using operators such as concatenation, assignment etc.

    Assignment

    Code:
    S = ‘Hello’
    S = ‘Yellow’
    
    The above code changed the value of the variable s and not the value of the string, what happens behind the scenes is that python declared 2 strings “Hello” and “Yellow” and these strings are saved somewhere in the memory, in the first line s points to the string “Hello” and in the second the string point to “Yellow”. These can be imagined as pointers in C.

    Concatenation

    Code:
    S = ‘Hello’
    S = S + ‘z’
    
    In the above code we first assign S the string “Hello” and then in the next line we set it to the concatenated output of Hello + z i.e Helloz.

    What Python is doing behind the scenes is that I declared 2 strings i.e ‘Hello’ , ‘z’ now after the concatenation the value of the string is not changed but instead it creates a new string, ‘Helloz’ and then by assignment S now points to this string.

    Conclusion



    The purpose of this article was to introduce the reader about the main differences in strings and lists especially, Mutation. The main motive was to provide a clear picture of what python is doing behind the scenes and what we can expect from that.
     
  2. Copywriter

    Copywriter New Member

    Joined:
    Mar 9, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://myfreelancecopywriter.com/copywriting-services/
    Thank you to share useful post. Its real ye very informative for me and other.
     
    Last edited by a moderator: Apr 28, 2012

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice