manipulating-strings.rst
changeset 217 b595f90016c5
parent 216 7206fe0c03c5
child 218 620a644c0581
equal deleted inserted replaced
216:7206fe0c03c5 217:b595f90016c5
     1 ========
       
     2  Script
       
     3 ========
       
     4 
       
     5 {{{ show the welcome slide }}}
       
     6 
       
     7 Welcome to this tutorial on manipulating strings. 
       
     8 
       
     9 {{{ show the slide with outline }}} 
       
    10 
       
    11 In this tutorial we shall learn to manipulate strings, specifically
       
    12 slicing and reversing them, or replacing characters, converting from
       
    13 upper to lower case and vice-versa 
       
    14 
       
    15 #[punch: reversed returns an iterator. should we still teach it?]
       
    16 
       
    17 We have an ``ipython`` shell open, in which we are going to work,
       
    18 through out this session. 
       
    19 
       
    20 Let us consider a simple problem, and learn how to slice strings and
       
    21 get sub-strings. 
       
    22 
       
    23 Let's say the variable ``week`` has the list of the names of the days
       
    24 of the week. 
       
    25 
       
    26 ::
       
    27 
       
    28     week = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
       
    29 
       
    30 
       
    31 Now given a string ``s``, we should be able to check if the string is a
       
    32 valid name of a day of the week or not. 
       
    33 
       
    34 ::
       
    35 
       
    36     s = saturday
       
    37 
       
    38 
       
    39 ``s`` could be in any of the forms --- sat, saturday, Sat, Saturday,
       
    40 SAT, SATURDAY. We shall now be solving the problem only for the forms,
       
    41 sat and saturday. We shall solve it for the other forms, at the end of
       
    42 the tutorial. 
       
    43 
       
    44 {{{ show these forms in a slide }}}
       
    45 
       
    46 So, we need to check if the first three characters of the given string
       
    47 exists in the variable ``week``. 
       
    48 
       
    49 As, with any of the string data-types, strings can be sliced into
       
    50 sub-strings. To get the first three characters of s, we say, 
       
    51 
       
    52 ::
       
    53 
       
    54     s[0:3]
       
    55 
       
    56 Note that, we are slicing the string from the index 0 to index 3, 3
       
    57 not included. 
       
    58 
       
    59 As we already know, the last element of the string can be accessed
       
    60 using ``s[-1]``.  
       
    61 
       
    62 %%1%% Pause the video here and obtain the sub-string excluding the
       
    63 first and last characters from the string. 
       
    64 
       
    65 ::
       
    66 
       
    67     s[1:-1]
       
    68 
       
    69 gives the a substring of s, without the first and the last
       
    70 characters. 
       
    71 
       
    72 ::
       
    73 
       
    74     s = saturday
       
    75     s[:3]
       
    76 
       
    77 Now, we just check if that substring is present in the variable
       
    78 ``week``. 
       
    79 
       
    80 ::
       
    81 
       
    82     s[:3] in week          
       
    83 
       
    84 Let us now consider the problem of finding out if a given string is
       
    85 palindromic or not. First of all, a palindromic string is a string
       
    86 that remains same even when it has been reversed.
       
    87 
       
    88 Let the string given be ``malayalam``.
       
    89 
       
    90 ::
       
    91 
       
    92     s = "malayalam"
       
    93 
       
    94 Now, we need to compare this string with it's reverse. 
       
    95 
       
    96 Again, we will use a technique common to all sequence data-types,
       
    97 [::-1]
       
    98 
       
    99 So, we obtain the reverse of s, by simply saying, 
       
   100 
       
   101 ::
       
   102 
       
   103     s[::-1]
       
   104 
       
   105 Now, to check if the string is ``s`` is palindromic, we say
       
   106 ::
       
   107 
       
   108     s == s[::-1]
       
   109 
       
   110 As, expected, we get ``True``. 
       
   111 
       
   112 Now, if the string we are given is ``Malayalam`` instead of
       
   113 ``malayalam``, the above comparison would return a False. So, we will
       
   114 have to convert the string to all lower case or all upper case, before
       
   115 comparing. Python provides methods, ``s.lower`` and ``s.upper`` to
       
   116 achieve this. 
       
   117 
       
   118 Let's try it out. 
       
   119 ::
       
   120 
       
   121    s = "Malayalam"
       
   122 
       
   123    s.upper()
       
   124 
       
   125    s
       
   126 
       
   127    s.lower()
       
   128 
       
   129    s.lower() == s.lower()[::-1]
       
   130    
       
   131 Note that these methods, do not change the original string, but return
       
   132 a new string.
       
   133 
       
   134 a%% %% Pause the video here, and finish the problem of checking if
       
   135 ``s`` is a valid name of a day of the week and then resume the
       
   136 video. Change the solution to this problem, to include forms like,
       
   137 SAT, SATURDAY, Saturday and Sat. 
       
   138 
       
   139 ::
       
   140 
       
   141     s.lower()[:3] in week
       
   142 
       
   143 We just convert any input string to lower case and then check if it is
       
   144 present in the list ``week``. 
       
   145 
       
   146 Now, let us consider another problem. We often encounter e-mail id's
       
   147 which have @ and periods replaced with text, something like
       
   148 info[at]fossee[dot]in. We now wish to get back proper e-mail
       
   149 addresses.  
       
   150 
       
   151 Let's say the variable email has the email address. 
       
   152 ::
       
   153 
       
   154    email = "info[at]fossee[dot]in"
       
   155 
       
   156 Now, we first replace the ``[at]`` with the ``@``, using the replace
       
   157 method of strings. 
       
   158 ::
       
   159 
       
   160    email = email.replace("[at]", "@")
       
   161    print email
       
   162 
       
   163 %%1%% Pause the video here and replace the ``[dot]`` with ``.`` and then
       
   164 resume the video. 
       
   165 
       
   166 ::
       
   167 
       
   168    email = email.replace("[dot]", ".")        
       
   169    print email
       
   170 
       
   171 
       
   172 That brings us to the end of the tutorial. 
       
   173 
       
   174 {{{ show summary slide }}}
       
   175 
       
   176 In this tutorial, we have learnt how to get substrings, reverse
       
   177 strings and a few useful methods, namely upper, lower and replace. 
       
   178 
       
   179 Thank You!