manipulating-strings/script.rst
changeset 266 8018779e02b7
parent 265 5734d03b026c
child 286 44f06ae0d957
equal deleted inserted replaced
265:5734d03b026c 266:8018779e02b7
     5 
     5 
     6 .. 1. Slice strings and get sub-strings out of them
     6 .. 1. Slice strings and get sub-strings out of them
     7 .. #. Reverse strings
     7 .. #. Reverse strings
     8 .. #. Replace characters in strings. 
     8 .. #. Replace characters in strings. 
     9 .. #. Convert to strings to upper or lower case
     9 .. #. Convert to strings to upper or lower case
       
    10 .. #. joining a list of strings
    10 
    11 
    11 .. Prerequisites
    12 .. Prerequisites
    12 .. -------------
    13 .. -------------
    13 
    14 
    14 ..   1. getting started with strings
    15 ..   1. getting started with strings
    15 ..   #. getting started with lists
    16 ..   #. getting started with lists
    16 ..   #.
    17 ..   #. basic datatypes
    17      
    18      
    18 .. Author              : Puneeth 
    19 .. Author              : Puneeth 
    19    Internal Reviewer   : 
    20    Internal Reviewer   : 
    20    External Reviewer   :
    21    External Reviewer   :
    21    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    22    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    29 
    30 
    30 {{{ show the slide with outline }}} 
    31 {{{ show the slide with outline }}} 
    31 
    32 
    32 In this tutorial we shall learn to manipulate strings, specifically
    33 In this tutorial we shall learn to manipulate strings, specifically
    33 slicing and reversing them, or replacing characters, converting from
    34 slicing and reversing them, or replacing characters, converting from
    34 upper to lower case and vice-versa 
    35 upper to lower case and vice-versa and joining a list of strings.
    35 
    36 
    36 #[punch: reversed returns an iterator. should we still teach it?]
    37 .. #[punch: reversed returns an iterator. should we still teach it?]
    37 
    38 
    38 We have an ``ipython`` shell open, in which we are going to work,
    39 We have an ``ipython`` shell open, in which we are going to work,
    39 through out this session. 
    40 through out this session. 
    40 
    41 
    41 Let us consider a simple problem, and learn how to slice strings and
    42 Let us consider a simple problem, and learn how to slice strings and
   197 ::
   198 ::
   198 
   199 
   199    email = email.replace("[dot]", ".")        
   200    email = email.replace("[dot]", ".")        
   200    print email
   201    print email
   201 
   202 
       
   203 Now, let's look at another interesting problem where we have a list of
       
   204 e-mail addresses and we wish to obtain one long string of e-mail
       
   205 addresses separated by commas or semi-colons. 
       
   206 
       
   207 ::
       
   208 
       
   209   email_list = ["info@fossee.in", "enquiries@fossee.in",  "help@fossee.in"]
       
   210 
       
   211 
       
   212 Now, if we wish to obtain one long string, separating each of the
       
   213 email id by a comma, we use the join operator on ``,``. 
       
   214 
       
   215 ::
       
   216 
       
   217   email_str = ", ".join(email_list)
       
   218   print email_str
       
   219 
       
   220 Notice that the email ids are joined by a comma followed by a space. 
       
   221 
       
   222 Following is an exercise that you must do. 
       
   223 
       
   224 %%3%% From the email_str that we generated, change the separator to be
       
   225 a semicolon instead of a comma. 
       
   226 
       
   227 Please, pause the video here. Do the exercise and then continue. 
       
   228 
       
   229 ::
       
   230 
       
   231   email_str = email_str.replace(",", ";")
   202 
   232 
   203 That brings us to the end of the tutorial. 
   233 That brings us to the end of the tutorial. 
   204 
   234 
   205 {{{ show summary slide }}}
   235 {{{ show summary slide }}}
   206 
   236 
   207 In this tutorial, we have learnt how to get substrings, reverse
   237 In this tutorial, we have learnt how to get substrings, reverse
   208 strings and a few useful methods, namely upper, lower and replace. 
   238 strings and a few useful methods, namely upper, lower, replace and
       
   239 join. 
   209 
   240 
   210 {{{ Show the "sponsored by FOSSEE" slide }}}
   241 {{{ Show the "sponsored by FOSSEE" slide }}}
   211 
   242 
   212 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   243 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   213 
   244