Python/C3/Manipulating-strings/English-timed

From Script | Spoken-Tutorial
Revision as of 17:38, 2 January 2013 by Minal (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Timing Narration
0:00 Hi and Welcome to this tutorial on 'manipulating strings'.
0:04 At the end of this tutorial, you will be able to,
  1. Slice strings and get sub-strings out of them.
  2. Reverse strings.
  3. Replace characters in strings.
  4. Convert strings to upper or lower case.
  5. Join a list of strings.
0:19 Before beginning this tutorial,we would suggest you to complete the tutorial on "getting started with strings", "getting started with lists" and "basic datatypes and operators".
0:31 Let us invoke our ipython interpreter
0:34 So type ipython in the command.
0:39 Let us consider a simple problem, and learn how to slice strings and get sub-strings.
0:46 Let's say the variable week has the list of the names of the days of the week.
0:52 that is week is equal to within square brackets in double quotes sunday sun then comma mon for monday ,then tuesday so tue,then wednesday thursday, friday and saturday.
1:15 Let us consider a simple problem.
1:18 So now given a string s, we should be able to check if the string is a valid name of a day of the week or not.
1:29 Let us define our string as saturday
1:32 So type in double quotes saturday
1:36 s is equal to saturday
1:41 s could be in any of the form
1:46 that is sat saturday, then s captial a t then capital s saturday then sat centre letter in capital then whole saturday in capital letters
2:05 For now, we shall be solving the problem only for the forms,small sat and small saturday.
2:16 We shall solve it for the other forms, at the end of the tutorial.
2:23 So, we need to check if the first three characters of the given string exists in the variable week.
2:31 As with any of the sequence data-types, strings can be sliced into sub-strings.
2:37 To get the first three characters of s, we say,
2:41 s in closing brackets, square brackets 0 colon 3 and hit enter.
2:48 Note that, we are slicing the string from the index 0 to index 3, 3 not included.
2:55 As we already know, the last element of the string can be accessed using s within brackets -1..
3:02 Pause the video here, try out the following exercise and resume the video.
3:09 Obtain the sub-string excluding the first and last characters from the string s.
3:16 Switch to the terminal for solution.
3:18 Type s in square brackets 1 colon -1 and hit enter.
3:30 We get the substring of s, without the first and the last characters of s.
3:37 Now let us check if a particular substring is present in the variable week.
3:41 We shall check for 'sat'.
3:46 So type s in square brackets colon 3 and hit enter.
3:52 then s colon 3 in week
4:09 We get the result as true.
4:11 Let us now consider the problem of finding out, if a given string is palindromic or not.
4:18 First of all, a palindromic string is a string that remains same even when it has been reversed.
4:26 Let the string given be malayalam.
4:30 So s1 is equal to malayalam
4:35 So malayalam should be in double quotes.
4:40 Now, we need to compare this string with it's reverse.
4:45 Again, we will use a technique common to all sequence data-types, that is, within square brackets colon colon -1.
4:55 So, we obtain the reverse of s, by simply saying,s1 and within square brackets colon colon -1 and hit enter.
5:06 Now, to check if the string is s is palindromic, we say s1 is equal to is equal to s1 in square brackets colon colon -1
5:23 As, expected, we get True.
5:26 Now, if the string we are given is Malayalam instead of malayalam, the above comparison would return a False.
5:36 So, we will have to convert the string to a lower case or to all upper case, before comparing.
5:43 Python provides methods, s dot lower and s dot upper to achieve this.
5:48 Let's try it out.
5:51 So type s1 is equal to malayalam in double quotes where m is capital.
6:03 then s1 dot upper then closing brackets.
6:10 then s1 will give u the output.
6:14 okay. So as you can see, s has not changed.
6:18 It is because, upper returns a new string.
6:20 It doesn't change the original string.
6:22 Similarly, type s1 dot lower closing brackets.
6:32 Then s1 dot lower closing brackets is equal to is equal to
6:40 s1 dot lower closing brackets within square bracket colon colon -1
6:51 So pause the video here, try out the following exercise and resume the video.
6:57 Check if s is a valid name of a day of the week.
7:02 Change the solution to this problem, to include forms like, SAT, SATURDAY, Saturday and Sat.
7:19 Switch to your terminal for solution
7:22 Type s in week. Then s dot lower closing bracket
7:33 then in square brackets colon 3
7:37 then in week
7:43 So, as you can see, now we can check for presence of s in week, in whichever format it is present -- capitalized, or all caps, full name or short form.
7:56 We just convert any input string to lower case and then check if it is present in the list week.
8:03 Now, let us consider another problem. |- |8:05 | We often encounter e-mail id's which have '@' and periods replaced with text, something like info[at]fossee[dot]in. |- |8:26 | We now wish to get back proper e-mail addresses.
8:30 Let's say the variable email has the email address.
8:34 So type email is equal to
8:38 within quotes double quotes info in square brackets at then fossee then again square bracket foss then dot then in.
8:48 Now, we first replace the [at] with the @, using the replace method of strings.
8:57 so type email is equal to email.replace
9:05 then in brackets double quotes @ at square brackets comma then again in square bracket
9:14 Now, type print email to get the output.
9:22 So pause the video here, try out the following exercise and resume the video.
9:30 Replace the [dot] with . in email
9:38 Switch to the terminal for solution
9:43 Type email is equal to email.replace within brackets in double quotes dot square brackets
10:02 then again in double quotes a dot.
10:07 Now, let us look at another interesting problem where we have a list of e-mail addresses and we wish to obtain one long string of e-mail addresses separated by commas or semi-colons.
10:23 So type email underscore list
10:30 is equal to within square brackets double quotes info at the rate fossee dot in, enquiries at the rate fossee dot in,then again comma in double quotes help at the rate fossee dot in
10:42 Now, if we wish to obtain one long string, separating each of the email id by a comma, we use the join operation on email_str is equal to in double quotes comma
dot join then in brackets email underscore list
11:13 then hit enter.
11:15 then print email underscore str for output.
11:22 so type print email underscore str
11:28 So now, notice that the email ids are joined by a comma followed by a space.
11:34 So now pause the video here, try out the following exercise and resume the video.
11:41 For the email underscore str that we generated, change the separator to be a semicolon instead of a comma.
11:51 Then for that now switch to the terminal for solution.
11:56 Type email underscore str is equal to email underscore str dot replace then in brackets in double quotes comma then in another double quotes semicolon.
12:14 Then type print email underscore str.
12:26 We see that the email ids are joined by a semicolon followed by a space.
12:32 This brings us to the end of this tutorial.
12:35 In this tutorial, we have learnt to, Obtain sub-strings and reverse of strings by using the index numbers
12:41 2. Use following functions - - upper() -- to obtain the upper case of a string - lower() -- to obtain the lower case of a string - replace() -- to replace a character by another one - join() -- to join a list of strings with an operator
12:57 Here are some self assessment questions for you to solve
13:01 1. Given a string s = "this is a string", how will you change it to "this isn't a list" ?
13:09 Given the string "F.R.I.E.N.D.S" in s, obtain the string "friends" where the friends in s is all capital letters separated by dots.
13:26 And the answers,
13:28 1. We will use the replace function to accomplish this.
13:31 so type s = s dot replace within brackets and double quotes string,again brackets and double quotes list.
13:40 then s = s dot replace within brackets and double quotes is, again brackets and double quotes isn't
13:51 We notice that every 'is' in the statement has been replaced by isn't.
13:59 2. In order to change the string to lower case, we use the method lower()
14:05 So type s in square brackets colon colon 2 dot lower closing brackets.


14:13 So we hope you have enjoyed this tutorial and found it useful.
14:16 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha