manipulating_strings/script.rst
changeset 522 d33698326409
equal deleted inserted replaced
521:88a01948450d 522:d33698326409
       
     1 .. Objectives
       
     2 .. ----------
       
     3 
       
     4 .. By the end of this tutorial, you will be able to
       
     5 
       
     6 .. 1. Slice strings and get sub-strings out of them
       
     7 .. #. Reverse strings
       
     8 .. #. Replace characters in strings. 
       
     9 .. #. Convert strings to upper or lower case
       
    10 .. #. joining a list of strings
       
    11 
       
    12 .. Prerequisites
       
    13 .. -------------
       
    14 
       
    15 ..   1. getting started with strings
       
    16 ..   #. getting started with lists
       
    17 ..   #. basic datatypes
       
    18      
       
    19 .. Author              : Puneeth 
       
    20    Internal Reviewer   : Amit 
       
    21    External Reviewer   :
       
    22    Language Reviewer   : Bhanukiran
       
    23    Checklist OK?       : <08-11-2010, Anand, OK> [2010-10-05]
       
    24 
       
    25 Script
       
    26 ------
       
    27 
       
    28 {{{ Show the slide containing title }}}
       
    29 
       
    30 Hello Friends. Welcome to this tutorial on manipulating strings. 
       
    31 
       
    32 {{{ show the slide with outline }}} 
       
    33 
       
    34 In this tutorial we shall learn to manipulate strings, specifically
       
    35 slicing and reversing them, or replacing characters, converting from
       
    36 upper to lower case and vice-versa and joining a list of strings.
       
    37 
       
    38 We have an ``ipython`` shell open, in which we are going to work,
       
    39 through out this session. 
       
    40 
       
    41 Let us consider a simple problem, and learn how to slice strings and
       
    42 get sub-strings. 
       
    43 
       
    44 Let's say the variable ``week`` has the list of the names of the days
       
    45 of the week. 
       
    46 
       
    47 ::
       
    48 
       
    49     week = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
       
    50 
       
    51 
       
    52 Now given a string ``s``, we should be able to check if the string is a
       
    53 valid name of a day of the week or not. 
       
    54 
       
    55 ::
       
    56 
       
    57     s = saturday
       
    58 
       
    59 
       
    60 ``s`` could be in any of the forms --- sat, saturday, Sat, Saturday,
       
    61 SAT, SATURDAY. For now, shall now be solving the problem only for the forms,
       
    62 sat and saturday. We shall solve it for the other forms, at the end of
       
    63 the tutorial. 
       
    64 
       
    65 {{{ show these forms in a slide }}}
       
    66 
       
    67 So, we need to check if the first three characters of the given string
       
    68 exists in the variable ``week``. 
       
    69 
       
    70 As, with any of the sequence data-types, strings can be sliced into
       
    71 sub-strings. To get the first three characters of s, we say,
       
    72 
       
    73 ::
       
    74 
       
    75     s[0:3]
       
    76 
       
    77 Note that, we are slicing the string from the index 0 to index 3, 3
       
    78 not included. 
       
    79 
       
    80 As we already know, the last element of the string can be accessed
       
    81 using ``s[-1]``.  
       
    82 
       
    83 Following is an exercise that you must do. 
       
    84 
       
    85 %%1%% Obtain the sub-string excluding the first and last characters
       
    86 from the string s. 
       
    87 
       
    88 Please, pause the video here. Do the exercise(s) and then continue. 
       
    89 
       
    90 ::
       
    91 
       
    92     s[1:-1]
       
    93 
       
    94 gives the substring of s, without the first and the last
       
    95 characters of s. 
       
    96 
       
    97 ::
       
    98 
       
    99     s = saturday
       
   100     s[:3]
       
   101 
       
   102 Now, we just check if that substring is present in the variable
       
   103 ``week``. 
       
   104 
       
   105 ::
       
   106 
       
   107     s[:3] in week          
       
   108 
       
   109 Let us now consider the problem of finding out if a given string is
       
   110 palindromic or not. First of all, a palindromic string is a string
       
   111 that remains same even when it has been reversed.
       
   112 
       
   113 Let the string given be ``malayalam``.
       
   114 
       
   115 ::
       
   116 
       
   117     s = "malayalam"
       
   118 
       
   119 Now, we need to compare this string with it's reverse. 
       
   120 
       
   121 Again, we will use a technique common to all sequence data-types,
       
   122 [::-1]
       
   123 
       
   124 So, we obtain the reverse of s, by simply saying, 
       
   125 
       
   126 ::
       
   127 
       
   128     s[::-1]
       
   129 
       
   130 Now, to check if the string is ``s`` is palindromic, we say
       
   131 ::
       
   132 
       
   133     s == s[::-1]
       
   134 
       
   135 As, expected, we get ``True``. 
       
   136 
       
   137 Now, if the string we are given is ``Malayalam`` instead of
       
   138 ``malayalam``, the above comparison would return a False. So, we will
       
   139 have to convert the string to all lower case or all upper case, before
       
   140 comparing. Python provides methods, ``s.lower`` and ``s.upper`` to
       
   141 achieve this. 
       
   142 
       
   143 Let's try it out. 
       
   144 ::
       
   145 
       
   146    s = "Malayalam"
       
   147 
       
   148    s.upper()
       
   149 
       
   150    s
       
   151 
       
   152 As you can see, s has not changed. It is because, ``upper`` returns a
       
   153 new string. It doesn't change the original string. 
       
   154 
       
   155 ::
       
   156 
       
   157    s.lower()
       
   158 
       
   159    s.lower() == s.lower()[::-1]
       
   160    
       
   161 Following is an exercise that you must do. 
       
   162 
       
   163 %%2%% Check if ``s`` is a valid name of a day of the week. Change the
       
   164 solution to this problem, to include forms like, SAT, SATURDAY,
       
   165 Saturday and Sat.
       
   166 
       
   167 Please, pause the video here. Do the exercise and then continue. 
       
   168 
       
   169 ::
       
   170 
       
   171     s in week
       
   172 
       
   173     s.lower()[:3] in week
       
   174 
       
   175 
       
   176 So, as you can see, now we can check for presence of ``s`` in
       
   177 ``week``, in whichever format it is present -- capitalized, or all
       
   178 caps, full name or short form.
       
   179 
       
   180 We just convert any input string to lower case and then check if it is
       
   181 present in the list ``week``. 
       
   182 
       
   183 Now, let us consider another problem. We often encounter e-mail id's
       
   184 which have @ and periods replaced with text, something like
       
   185 info[at]fossee[dot]in. We now wish to get back proper e-mail
       
   186 addresses.  
       
   187 
       
   188 Let's say the variable email has the email address. 
       
   189 ::
       
   190 
       
   191    email = "info[at]fossee[dot]in"
       
   192 
       
   193 Now, we first replace the ``[at]`` with the ``@``, using the replace
       
   194 method of strings. 
       
   195 ::
       
   196 
       
   197    email = email.replace("[at]", "@")
       
   198    print email
       
   199 
       
   200 Following is an exercise that you must do. 
       
   201 
       
   202 %%3%% Replace the ``[dot]`` with ``.`` in ``email``
       
   203 
       
   204 Please, pause the video here. Do the exercise and then continue. 
       
   205 
       
   206 ::
       
   207 
       
   208    email = email.replace("[dot]", ".")        
       
   209    print email
       
   210 
       
   211 Now, let's look at another interesting problem where we have a list of
       
   212 e-mail addresses and we wish to obtain one long string of e-mail
       
   213 addresses separated by commas or semi-colons. 
       
   214 
       
   215 ::
       
   216 
       
   217   email_list = ["info@fossee.in", "enquiries@fossee.in",  "help@fossee.in"]
       
   218 
       
   219 
       
   220 Now, if we wish to obtain one long string, separating each of the
       
   221 email id by a comma, we use the join operator on ``,``. 
       
   222 
       
   223 ::
       
   224 
       
   225   email_str = ", ".join(email_list)
       
   226   print email_str
       
   227 
       
   228 Notice that the email ids are joined by a comma followed by a space. 
       
   229 
       
   230 Following is an exercise that you must do. 
       
   231 
       
   232 %%3%% From the email_str that we generated, change the separator to be
       
   233 a semicolon instead of a comma. 
       
   234 
       
   235 Please, pause the video here. Do the exercise and then continue. 
       
   236 
       
   237 ::
       
   238 
       
   239   email_str = email_str.replace(",", ";")
       
   240 
       
   241 That brings us to the end of the tutorial. 
       
   242 
       
   243 {{{ show summary slide }}}
       
   244 
       
   245 In this tutorial, we have learnt how to get substrings, reverse
       
   246 strings and a few useful methods, namely upper, lower, replace and
       
   247 join. 
       
   248 
       
   249 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   250 
       
   251 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   252 
       
   253 Hope you have enjoyed and found it useful.
       
   254 Thank you!
       
   255