manipulating-strings/script.rst
changeset 381 5415cb1bb4af
parent 350 d14bc84feca1
child 454 0c69c2b32183
equal deleted inserted replaced
380:c17aa604468a 381:5415cb1bb4af
    32 
    32 
    33 In this tutorial we shall learn to manipulate strings, specifically
    33 In this tutorial we shall learn to manipulate strings, specifically
    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?]
       
    38 
       
    39 
       
    40 We have an ``ipython`` shell open, in which we are going to work,
    37 We have an ``ipython`` shell open, in which we are going to work,
    41 through out this session. 
    38 through out this session. 
    42 
    39 
    43 Let us consider a simple problem, and learn how to slice strings and
    40 Let us consider a simple problem, and learn how to slice strings and
    44 get sub-strings. 
    41 get sub-strings. 
    67 {{{ show these forms in a slide }}}
    64 {{{ show these forms in a slide }}}
    68 
    65 
    69 So, we need to check if the first three characters of the given string
    66 So, we need to check if the first three characters of the given string
    70 exists in the variable ``week``. 
    67 exists in the variable ``week``. 
    71 
    68 
    72 As, with any of the string data-types, strings can be sliced into
    69 As, with any of the sequence data-types, strings can be sliced into
    73 .. #[Amit: Sequence data type???]
    70 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, 
       
    75 
    71 
    76 ::
    72 ::
    77 
    73 
    78     s[0:3]
    74     s[0:3]
    79 
    75 
    82 
    78 
    83 As we already know, the last element of the string can be accessed
    79 As we already know, the last element of the string can be accessed
    84 using ``s[-1]``.  
    80 using ``s[-1]``.  
    85 
    81 
    86 Following is an exercise that you must do. 
    82 Following is an exercise that you must do. 
    87 .. #[Amit: I don't know I am not sure about the sentence formation.]
    83 
    88 %%1%% Obtain the sub-string excluding the first and last characters
    84 %%1%% Obtain the sub-string excluding the first and last characters
    89 from the string s. 
    85 from the string s. 
    90 
    86 
    91 Please, pause the video here. Do the exercise(s) and then continue. 
    87 Please, pause the video here. Do the exercise(s) and then continue. 
    92 
    88 
   127 So, we obtain the reverse of s, by simply saying, 
   123 So, we obtain the reverse of s, by simply saying, 
   128 
   124 
   129 ::
   125 ::
   130 
   126 
   131     s[::-1]
   127     s[::-1]
   132 .. #[amit: I think using reversed in not required after this]
   128 
   133 Now, to check if the string is ``s`` is palindromic, we say
   129 Now, to check if the string is ``s`` is palindromic, we say
   134 ::
   130 ::
   135 
   131 
   136     s == s[::-1]
   132     s == s[::-1]
   137 
   133 
   150 
   146 
   151    s.upper()
   147    s.upper()
   152 
   148 
   153    s
   149    s
   154 
   150 
       
   151 As you can see, s has not changed. It is because, ``upper`` returns a
       
   152 new string. It doesn't change the original string. 
       
   153 
       
   154 ::
       
   155 
   155    s.lower()
   156    s.lower()
   156 
   157 
   157    s.lower() == s.lower()[::-1]
   158    s.lower() == s.lower()[::-1]
   158    
   159    
   159 Note that these methods, do not change the original string, but return
       
   160 a new string.
       
   161 
       
   162 .. #[amit: I wish we could include this right when s.upper() is used so 
       
   163 .. that it is clear]
       
   164 
       
   165 Following is an exercise that you must do. 
   160 Following is an exercise that you must do. 
   166 
   161 
   167 %%2%% Check if ``s`` is a valid name of a day of the week. Change the
   162 %%2%% Check if ``s`` is a valid name of a day of the week. Change the
   168 solution to this problem, to include forms like, SAT, SATURDAY,
   163 solution to this problem, to include forms like, SAT, SATURDAY,
   169 Saturday and Sat.
   164 Saturday and Sat.
   174 
   169 
   175     s in week
   170     s in week
   176 
   171 
   177     s.lower()[:3] in week
   172     s.lower()[:3] in week
   178 
   173 
   179 .. #[amit: May be a sentence or two about what our original problem was and 
   174 
   180 .. how this helps in solving it. One can loose the flow.]
   175 So, as you can see, now we can check for presence of ``s`` in
       
   176 ``week``, in whichever format it is present -- capitalized, or all
       
   177 caps, full name or short form.
       
   178 
   181 We just convert any input string to lower case and then check if it is
   179 We just convert any input string to lower case and then check if it is
   182 present in the list ``week``. 
   180 present in the list ``week``. 
   183 
   181 
   184 Now, let us consider another problem. We often encounter e-mail id's
   182 Now, let us consider another problem. We often encounter e-mail id's
   185 which have @ and periods replaced with text, something like
   183 which have @ and periods replaced with text, something like