basic_python/strings_dicts.rst
changeset 65 0f25f22a2725
child 67 5076574b7b83
equal deleted inserted replaced
53:76facb1dc81b 65:0f25f22a2725
       
     1 =======
       
     2 Strings
       
     3 =======
       
     4 
       
     5 Strings were briefly introduced previously in the introduction document. In this
       
     6 section strings will be presented in greater detail. All the standard operations 
       
     7 that can be performed on sequences such as indexing, slicing, multiplication, length
       
     8 minimum and maximum can be performed on string variables as well. One thing to
       
     9 be noted is that strings are immutable, which means that string variables are
       
    10 unchangeable. Hence, all item and slice assignments on strings are illegal.
       
    11 Let us look at a few example.
       
    12 
       
    13 ::
       
    14 
       
    15   >>> name = 'PythonFreak'
       
    16   >>> print name[3]
       
    17   h
       
    18   >>> print name[-1]
       
    19   k
       
    20   >>> print name[6:]
       
    21   Freak
       
    22   >>> name[6:0] = 'Maniac'
       
    23   Traceback (most recent call last):
       
    24     File "<stdin>", line 1, in <module>
       
    25   TypeError: 'str' object does not support item assignment
       
    26 
       
    27 This is quite expected, since string objects are immutable as already mentioned.
       
    28 The error message is clear in mentioning that 'str' object does not support item
       
    29 assignment.
       
    30 
       
    31 String Formatting
       
    32 =================
       
    33