getting-started-strings/script.rst
changeset 436 e62bc810999c
parent 313 b9b7bfce773e
child 513 c1f89e89d89c
equal deleted inserted replaced
435:975677bf1b8a 436:e62bc810999c
    28 Hello friends. Welcome to this spoken tutorial on Getting started with
    28 Hello friends. Welcome to this spoken tutorial on Getting started with
    29 strings.
    29 strings.
    30 
    30 
    31 {{{ Show the slide containing the outline }}}
    31 {{{ Show the slide containing the outline }}}
    32 
    32 
    33 In this tutorial, we will learn what do we actually mean by strings in
    33 In this tutorial, we will look at what we really mean by strings, how
    34 python, how python supports the use of strings. We will also learn
    34 python supports the use of strings and some of the operations that can
    35 some of the operations that can be performed on strings.
    35 be performed on strings. 
    36 
    36 
    37 {{{ Shift to terminal and start ipython }}}
    37 {{{ Shift to terminal and start ipython }}}
    38 
    38 
    39 To begin with let us start ipython, by typing::
    39 To begin with let us start ipython, by typing::
    40 
    40 
    41   ipython
    41   ipython
    42 
    42 
    43 on the terminal
    43 on the terminal
    44 
    44 
    45 So what are strings? In Python anything within either single quotes
    45 So, what are strings? In Python anything within either single quotes
    46 or double quotes or triple single quotes or triple double quotes are
    46 or double quotes or triple single quotes or triple double quotes are
    47 strings. This is true whatsoever, even if there is only one character
    47 strings. 
    48 within the quotes
       
    49 
    48 
    50 {{{ Type in ipython the following and read them as you type }}}::
    49 {{{ Type in ipython the following and read them as you type }}}::
    51 
    50 
    52   'This is a string'
    51   'This is a string'
    53   "This is a string too'
    52   "This is a string too'
    54   '''This is a string as well'''
    53   '''This is a string as well'''
    55   """This is also a string"""
    54   """This is also a string"""
    56   'p'
    55   'p'
       
    56   ""
    57 
    57 
    58 Having more than one control character to define strings come as very
    58 Note that it really doesn't matter how many characters are present in
    59 handy when one of the control characters itself is part of the
    59 the string. The last example is a null string or an empty string. 
    60 string. For example::
    60 
       
    61 Having more than one control character to define strings is handy when
       
    62 one of the control characters itself is part of the string. For
       
    63 example::
    61 
    64 
    62   "Python's string manipulation functions are very useful"
    65   "Python's string manipulation functions are very useful"
    63 
    66 
    64 In this case we use single quote for apostrophe. If we had only single
    67 By having multiple control characters, we avoid the need for
    65 quote to define strings we should have a clumsy way of escaping the
    68 escaping characters -- in this case the apostrophe. 
    66 single quote character to make it part of the string. Hence this is a
       
    67 very handy feature.
       
    68 
    69 
    69 The triple quoted strings let us define multi-lines strings without
    70 The triple quoted strings let us define multi-line strings without
    70 using any escaping. Everything within the triple quotes is a single
    71 using any escaping. Everything within the triple quotes is a single
    71 string no matter how many lines it extends::
    72 string no matter how many lines it extends::
    72 
    73 
    73    """Having more than one control character to define
    74    """Having more than one control character to define
    74    strings come as very handy when one of the control
    75    strings come as very handy when one of the control
    97   a * 5
    98   a * 5
    98 
    99 
    99 gives another string in which the original string 'Hello' is repeated
   100 gives another string in which the original string 'Hello' is repeated
   100 5 times.
   101 5 times.
   101 
   102 
   102 Since strings are collections we can access individual items in the
   103 Following is an exercise that you must do. 
   103 string using the subscripts::
   104 
       
   105 %% %% Obtain the string ``%% -------------------- %%`` (20 hyphens)
       
   106       without typing out all the twenty hyphens. 
       
   107 
       
   108 Please, pause the video here. Do the exercise and then continue. 
       
   109 
       
   110 ::
       
   111 
       
   112   s = "%% " + "-"*20 + " %%"
       
   113 
       
   114 Let's now look at accessing individual elements of strings. Since,
       
   115 strings are collections we can access individual items in the string
       
   116 using the subscripts::
   104 
   117 
   105   a[0]
   118   a[0]
   106 
   119 
   107 gives us the first character in the string. The indexing starts from 0
   120 gives us the first character in the string. The indexing starts from 0
   108 for the first character up to n-1 for the last character. We can
   121 for the first character and goes up to n-1 for the last character. We
   109 access the strings from the end using negative indices::
   122 can access the strings from the end using negative indices::
   110 
   123 
   111   a[-2]
   124   a[-1]
       
   125 
       
   126 gives us the last element of the string and 
       
   127 ::
       
   128 
       
   129     a[-2]
   112 
   130 
   113 gives us second element from the end of the string
   131 gives us second element from the end of the string
       
   132 
       
   133 Following is an exercise that you must do. 
       
   134 
       
   135 %% %% Given a string, ``s = "Hello World"``, what is the output of::
       
   136 
       
   137       s[-5] 
       
   138       s[-10]
       
   139       s[-15]
       
   140 
       
   141 Please, pause the video here. Do the exercise and then continue. 
       
   142 
       
   143 ::
       
   144 
       
   145   s[-5] 
       
   146 
       
   147 gives us 'W'
       
   148 ::
       
   149 
       
   150   s[-10] 
       
   151 
       
   152 gives us 'e' and 
       
   153 ::
       
   154 
       
   155   s[-15] 
       
   156 
       
   157 gives us an ``IndexError``, as should be expected, since the string
       
   158 given to us is only 11 characters long. 
   114 
   159 
   115 Let us attempt to change one of the characters in a string::
   160 Let us attempt to change one of the characters in a string::
   116 
   161 
   117   a = 'hello'
   162   a = 'hello'
   118   a[0] = 'H'
   163   a[0] = 'H'
   130 This brings us to the end of another session. In this tutorial session
   175 This brings us to the end of another session. In this tutorial session
   131 we learnt
   176 we learnt
   132 
   177 
   133   * How to define strings
   178   * How to define strings
   134   * Different ways of defining a string
   179   * Different ways of defining a string
   135   * String concatenation and repeatition
   180   * String concatenation and repetition
   136   * Accessing individual elements of the string
   181   * Accessing individual elements of the string
   137   * Immutability of strings
   182   * Immutability of strings
   138 
   183 
   139 {{{ Show the "sponsored by FOSSEE" slide }}}
   184 {{{ Show the "sponsored by FOSSEE" slide }}}
   140 
   185