manipulating-strings/script.rst
changeset 350 d14bc84feca1
parent 286 44f06ae0d957
child 381 5415cb1bb4af
equal deleted inserted replaced
349:9ced58c5c3b6 350:d14bc84feca1
    15 ..   1. getting started with strings
    15 ..   1. getting started with strings
    16 ..   #. getting started with lists
    16 ..   #. getting started with lists
    17 ..   #. basic datatypes
    17 ..   #. basic datatypes
    18      
    18      
    19 .. Author              : Puneeth 
    19 .. Author              : Puneeth 
    20    Internal Reviewer   : 
    20    Internal Reviewer   : Amit 
    21    External Reviewer   :
    21    External Reviewer   :
    22    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    22    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    23 
    23 
    24 Script
    24 Script
    25 ------
    25 ------
    34 slicing and reversing them, or replacing characters, converting from
    34 slicing and reversing them, or replacing characters, converting from
    35 upper to lower case and vice-versa and joining a list of strings.
    35 upper to lower case and vice-versa and joining a list of strings.
    36 
    36 
    37 .. #[punch: reversed returns an iterator. should we still teach it?]
    37 .. #[punch: reversed returns an iterator. should we still teach it?]
    38 
    38 
       
    39 
    39 We have an ``ipython`` shell open, in which we are going to work,
    40 We have an ``ipython`` shell open, in which we are going to work,
    40 through out this session. 
    41 through out this session. 
    41 
    42 
    42 Let us consider a simple problem, and learn how to slice strings and
    43 Let us consider a simple problem, and learn how to slice strings and
    43 get sub-strings. 
    44 get sub-strings. 
    57 
    58 
    58     s = saturday
    59     s = saturday
    59 
    60 
    60 
    61 
    61 ``s`` could be in any of the forms --- sat, saturday, Sat, Saturday,
    62 ``s`` could be in any of the forms --- sat, saturday, Sat, Saturday,
    62 SAT, SATURDAY. We shall now be solving the problem only for the forms,
    63 SAT, SATURDAY. For now, shall now be solving the problem only for the forms,
    63 sat and saturday. We shall solve it for the other forms, at the end of
    64 sat and saturday. We shall solve it for the other forms, at the end of
    64 the tutorial. 
    65 the tutorial. 
    65 
    66 
    66 {{{ show these forms in a slide }}}
    67 {{{ show these forms in a slide }}}
    67 
    68 
    68 So, we need to check if the first three characters of the given string
    69 So, we need to check if the first three characters of the given string
    69 exists in the variable ``week``. 
    70 exists in the variable ``week``. 
    70 
    71 
    71 As, with any of the string data-types, strings can be sliced into
    72 As, with any of the string data-types, strings can be sliced into
       
    73 .. #[Amit: Sequence data type???]
    72 sub-strings. To get the first three characters of s, we say, 
    74 sub-strings. To get the first three characters of s, we say, 
    73 
    75 
    74 ::
    76 ::
    75 
    77 
    76     s[0:3]
    78     s[0:3]
    80 
    82 
    81 As we already know, the last element of the string can be accessed
    83 As we already know, the last element of the string can be accessed
    82 using ``s[-1]``.  
    84 using ``s[-1]``.  
    83 
    85 
    84 Following is an exercise that you must do. 
    86 Following is an exercise that you must do. 
    85 
    87 .. #[Amit: I don't know I am not sure about the sentence formation.]
    86 %%1%% Obtain the sub-string excluding the first and last characters
    88 %%1%% Obtain the sub-string excluding the first and last characters
    87 from the string s. 
    89 from the string s. 
    88 
    90 
    89 Please, pause the video here. Do the exercise(s) and then continue. 
    91 Please, pause the video here. Do the exercise(s) and then continue. 
    90 
    92 
   125 So, we obtain the reverse of s, by simply saying, 
   127 So, we obtain the reverse of s, by simply saying, 
   126 
   128 
   127 ::
   129 ::
   128 
   130 
   129     s[::-1]
   131     s[::-1]
   130 
   132 .. #[amit: I think using reversed in not required after this]
   131 Now, to check if the string is ``s`` is palindromic, we say
   133 Now, to check if the string is ``s`` is palindromic, we say
   132 ::
   134 ::
   133 
   135 
   134     s == s[::-1]
   136     s == s[::-1]
   135 
   137 
   155    s.lower() == s.lower()[::-1]
   157    s.lower() == s.lower()[::-1]
   156    
   158    
   157 Note that these methods, do not change the original string, but return
   159 Note that these methods, do not change the original string, but return
   158 a new string.
   160 a new string.
   159 
   161 
       
   162 .. #[amit: I wish we could include this right when s.upper() is used so 
       
   163 .. that it is clear]
       
   164 
   160 Following is an exercise that you must do. 
   165 Following is an exercise that you must do. 
   161 
   166 
   162 %%2%% Check if ``s`` is a valid name of a day of the week. Change the
   167 %%2%% Check if ``s`` is a valid name of a day of the week. Change the
   163 solution to this problem, to include forms like, SAT, SATURDAY,
   168 solution to this problem, to include forms like, SAT, SATURDAY,
   164 Saturday and Sat.
   169 Saturday and Sat.
   169 
   174 
   170     s in week
   175     s in week
   171 
   176 
   172     s.lower()[:3] in week
   177     s.lower()[:3] in week
   173 
   178 
       
   179 .. #[amit: May be a sentence or two about what our original problem was and 
       
   180 .. how this helps in solving it. One can loose the flow.]
   174 We just convert any input string to lower case and then check if it is
   181 We just convert any input string to lower case and then check if it is
   175 present in the list ``week``. 
   182 present in the list ``week``. 
   176 
   183 
   177 Now, let us consider another problem. We often encounter e-mail id's
   184 Now, let us consider another problem. We often encounter e-mail id's
   178 which have @ and periods replaced with text, something like
   185 which have @ and periods replaced with text, something like