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