getting_started_with_strings/script.rst
changeset 522 d33698326409
equal deleted inserted replaced
521:88a01948450d 522:d33698326409
       
     1 .. Objectives
       
     2 .. ----------
       
     3 
       
     4 .. At the end of this tutorial, you should know --
       
     5 
       
     6 ..   1. How to define strings
       
     7 ..   #. Different ways of defining a string
       
     8 ..   #. How to concatenate strings 
       
     9 ..   #. How to print a string repeatedly 
       
    10 ..   #. Accessing individual elements of the string
       
    11 ..   #. Immutability of strings
       
    12 
       
    13 .. Prerequisites
       
    14 .. -------------
       
    15 
       
    16 .. 1. getting started with ipython
       
    17      
       
    18 .. Author              : Madhu
       
    19    Internal Reviewer   : Punch
       
    20    External Reviewer   :
       
    21    Language Reviewer   : Bhanukiran
       
    22    Checklist OK?       : <15-11-2010, Anand, OK> [2010-10-05]
       
    23 
       
    24 Script
       
    25 ------
       
    26 
       
    27 {{{ Show the slide containing the title }}}
       
    28 
       
    29 Hello friends. Welcome to this spoken tutorial on Getting started with
       
    30 strings.
       
    31 
       
    32 {{{ Show the slide containing the outline }}}
       
    33 
       
    34 In this tutorial, we will look at what we really mean by strings, how
       
    35 Python supports the use of strings and some of the operations that can
       
    36 be performed on strings. 
       
    37 
       
    38 {{{ Shift to terminal and start ipython }}}
       
    39 
       
    40 To begin with let us start ipython, by typing::
       
    41 
       
    42   ipython
       
    43 
       
    44 on the terminal
       
    45 
       
    46 So, what are strings? In Python anything within either single quotes
       
    47 or double quotes or triple single quotes or triple double quotes are
       
    48 strings. 
       
    49 
       
    50 {{{ Type in ipython the following and read them as you type }}}::
       
    51 
       
    52   'This is a string'
       
    53   "This is a string too'
       
    54   '''This is a string as well'''
       
    55   """This is also a string"""
       
    56   'p'
       
    57   ""
       
    58 
       
    59 Note that it really doesn't matter how many characters are present in
       
    60 the string. The last example is a null string or an empty string. 
       
    61 
       
    62 Having more than one control character to define strings is handy when
       
    63 one of the control characters itself is part of the string. For
       
    64 example::
       
    65 
       
    66   "Python's string manipulation functions are very useful"
       
    67 
       
    68 By having multiple control characters, we avoid the need for
       
    69 escaping characters -- in this case the apostrophe. 
       
    70 
       
    71 The triple quoted strings let us define multi-line strings without
       
    72 using any escaping. Everything within the triple quotes is a single
       
    73 string no matter how many lines it extends::
       
    74 
       
    75    """Having more than one control character to define
       
    76    strings come as very handy when one of the control
       
    77    characters itself is part of the string."""
       
    78 
       
    79 We can assign this string to any variable::
       
    80 
       
    81   a = 'Hello, World!'
       
    82 
       
    83 Now 'a' is a string variable. String is a collection of characters. In
       
    84 addition string is an immutable collection. So all the operations that
       
    85 are applicable to any other immutable collection in Python works on
       
    86 string as well. So we can add two strings::
       
    87 
       
    88   a = 'Hello'
       
    89   b = 'World'
       
    90   c = a + ', ' + b + '!'
       
    91 
       
    92 We can add string variables as well as the strings themselves all in
       
    93 the same statement. The addition operation performs the concatenation
       
    94 of two strings.
       
    95 
       
    96 Similarly we can multiply a string with an integer::
       
    97 
       
    98   a = 'Hello'
       
    99   a * 5
       
   100 
       
   101 gives another string in which the original string 'Hello' is repeated
       
   102 5 times.
       
   103 
       
   104 Following is an exercise that you must do. 
       
   105 
       
   106 %% %% Obtain the string ``%% -------------------- %%`` (20 hyphens)
       
   107       without typing out all the twenty hyphens. 
       
   108 
       
   109 Please, pause the video here. Do the exercise and then continue. 
       
   110 
       
   111 ::
       
   112 
       
   113   s = "%% " + "-"*20 + " %%"
       
   114 
       
   115 Let's now look at accessing individual elements of strings. Since,
       
   116 strings are collections we can access individual items in the string
       
   117 using the subscripts::
       
   118 
       
   119   a[0]
       
   120 
       
   121 gives us the first character in the string. The indexing starts from 0
       
   122 for the first character and goes up to n-1 for the last character. We
       
   123 can access the strings from the end using negative indices::
       
   124 
       
   125   a[-1]
       
   126 
       
   127 gives us the last element of the string and 
       
   128 ::
       
   129 
       
   130     a[-2]
       
   131 
       
   132 gives us second element from the end of the string
       
   133 
       
   134 Following is an exercise that you must do. 
       
   135 
       
   136 %% %% Given a string, ``s = "Hello World"``, what is the output of::
       
   137 
       
   138       s[-5] 
       
   139       s[-10]
       
   140       s[-15]
       
   141 
       
   142 Please, pause the video here. Do the exercise and then continue. 
       
   143 
       
   144 ::
       
   145 
       
   146   s[-5] 
       
   147 
       
   148 gives us 'W'
       
   149 ::
       
   150 
       
   151   s[-10] 
       
   152 
       
   153 gives us 'e' and 
       
   154 ::
       
   155 
       
   156   s[-15] 
       
   157 
       
   158 gives us an ``IndexError``, as should be expected, since the string
       
   159 given to us is only 11 characters long. 
       
   160 
       
   161 Let us attempt to change one of the characters in a string::
       
   162 
       
   163   a = 'hello'
       
   164   a[0] = 'H'
       
   165 
       
   166 As said earlier, strings are immutable. We cannot manipulate a
       
   167 string. Although there are some methods which let us manipulate
       
   168 strings, we will look at them in the advanced session on strings. In
       
   169 addition to the methods that let us manipulate the strings we have
       
   170 methods like split which lets us break the string on the specified
       
   171 separator, the join method which lets us combine the list of strings
       
   172 into a single string based on the specified separator.
       
   173 
       
   174 {{{ Show summary slide }}}
       
   175 
       
   176 This brings us to the end of another session. In this tutorial session
       
   177 we learnt
       
   178 
       
   179   * How to define strings
       
   180   * Different ways of defining a string
       
   181   * String concatenation and repetition
       
   182   * Accessing individual elements of the string
       
   183   * Immutability of strings
       
   184 
       
   185 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   186 
       
   187 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   188 
       
   189 Hope you have enjoyed and found it useful.
       
   190 Thank you!
       
   191