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 |
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 |