1 00:00:00 --> 00:00:03 Hi and Welcome to this tutorial on 'manipulating strings'. 2 00:00:04 --> 00:00:18 At the end of this tutorial, you will be able to, Slice strings and get sub-strings out of them. Reverse strings. Replace characters in strings. Convert strings to upper or lower case. Join a list of strings. 3 00:00:19 --> 00:00:30 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". 4 00:00:31 --> 00:00:33 Let us invoke our ipython interpreter 5 00:00:34 --> 00:00:38 So type ipython in the command. 6 00:00:39 --> 00:00:45 Let us consider a simple problem, and learn how to slice strings and get sub-strings. 7 00:00:46 --> 00:00:51 Let's say the variable week has the list of the names of the days of the week. 8 00:00:52 --> 00:01:14 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. 9 00:01:15 --> 00:01:17 Let us consider a simple problem. 10 00:01:18 --> 00:01:28 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. 11 00:01:29 --> 00:01:31 Let us define our string as saturday 12 00:01:32 --> 00:01:35 So type in double quotes saturday 13 00:01:36 --> 00:01:40 s is equal to saturday 14 00:01:41 --> 00:01:45 s could be in any of the form 15 00:01:46 --> 00:02:04 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 16 00:02:05 --> 00:02:15 For now, we shall be solving the problem only for the forms,small sat and small saturday. 17 00:02:16 --> 00:02:22 We shall solve it for the other forms, at the end of the tutorial. 18 00:02:23 --> 00:02:30 So, we need to check if the first three characters of the given string exists in the variable week . 19 00:02:31 --> 00:02:36 As with any of the sequence data-types, strings can be sliced into sub-strings. 20 00:02:37 --> 00:02:40 To get the first three characters of s, we say, 21 00:02:41 --> 00:02:47 s in closing brackets, square brackets 0 colon 3 and hit enter. 22 00:02:48 --> 00:02:54 Note that, we are slicing the string from the index 0 to index 3, 3 not included. 23 00:02:55 --> 00:03:01 As we already know, the last element of the string can be accessed using s within brackets -1 24 00:03:02 --> 00:03:08 Pause the video here, try out the following exercise and resume the video. 25 00:03:09 --> 00:03:15 Obtain the sub-string excluding the first and last characters from the string s. 26 00:03:16 --> 00:03:17 Switch to the terminal for solution. 27 00:03:18 --> 00:03:29 Type s in square brackets 1 colon -1 and hit enter. 28 00:03:30 --> 00:03:36 We get the substring of s, without the first and the last characters of s. 29 00:03:37 --> 00:03:40 Now let us check if a particular substring is present in the variable week . 30 00:03:41 --> 00:03:45 We shall check for 'sat'. 31 00:03:46 --> 00:03:51 So type s in square brackets colon 3 and hit enter. 32 00:03:52 --> 00:04:08 then s colon 3 in week 33 00:04:09 --> 00:04:10 We get the result as true. 34 00:04:11 --> 00:04:17 Let us now consider the problem of finding out, if a given string is palindromic or not. 35 00:04:18 --> 00:04:25 First of all, a palindromic string is a string that remains same even when it has been reversed. 36 00:04:26 --> 00:04:29 Let the string given be malayalam 37 00:04:30 --> 00:04:34 So s1 is equal to malayalam 38 00:04:35 --> 00:04:39 So malayalam should be in double quotes. 39 00:04:40 --> 00:04:44 Now, we need to compare this string with it's reverse. 40 00:04:45 --> 00:04:54 Again, we will use a technique common to all sequence data-types, that is, within square brackets colon colon -1 41 00:04:55 --> 00:05:05 So, we obtain the reverse of s, by simply saying,s1 and within square brackets colon colon -1 and hit enter. 42 00:05:06 --> 00:05:22 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 43 00:05:23 --> 00:05:25 As, expected, we get True. 44 00:05:26 --> 00:05:35 Now, if the string we are given is Malayalam instead of malayalam , the above comparison would return a False. 45 00:05:36 --> 00:05:42 So, we will have to convert the string to a lower case or to all upper case, before comparing. 46 00:05:43 --> 00:05:47 Python provides methods, s dot lower and s dot upper to achieve this. 47 00:05:48 --> 00:05:50 Let's try it out. 48 00:05:51 --> 00:06:02 So type s1 is equal to Malayalam in double quotes where m is capital. 49 00:06:03 --> 00:06:09 then s1 dot upper then closing brackets. 50 00:06:10 --> 00:06:13 then s1 will give u the output. 51 00:06:14 --> 00:06:17 okay. So as you can see, s has not changed. 52 00:06:18 --> 00:06:19 It is because, upper returns a new string. 53 00:06:20 --> 00:06:21 It doesn't change the original string. 54 00:06:22 --> 00:06:31 Similarly, type s1 dot lower closing brackets. 55 00:06:32 --> 00:06:39 Then s1 dot lower closing brackets is equal to is equal to 56 00:06:40 --> 00:06:50 s1 dot lower closing brackets within square bracket colon colon -1 57 00:06:51 --> 00:06:56 So pause the video here, try out the following exercise and resume the video. 58 00:06:57 --> 00:07:01 Check if s is a valid name of a day of the week. 59 00:07:02 --> 00:07:18 Change the solution to this problem, to include forms like, SAT, SATURDAY, Saturday and Sat. 60 00:07:19 --> 00:07:21 Switch to your terminal for solution 61 00:07:22 --> 00:07:32 Type s in week. Then s dot lower closing bracket 62 00:07:33 --> 00:07:36 then in square brackets colon 3 63 00:07:37 --> 00:07:42 then in week 64 00:07:43 --> 00:07:55 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. 65 00:07:56 --> 00:08:02 We just convert any input string to lower case and then check if it is present in the list week. 66 00:08:03 --> 00:08:29 Now, let us consider another problem. |- |08:05 | We often encounter e-mail id's which have '@' and periods replaced with text, something like info[at]fossee[dot]in. |- |08:26 | We now wish to get back proper e-mail addresses. 67 00:08:30 --> 00:08:33 Let's say the variable email has the email address. 68 00:08:34 --> 00:08:37 So type email is equal to 69 00:08:38 --> 00:08:47 within quotes double quotes info in square brackets at then fossee then again square bracket foss then dot then in. 70 00:08:48 --> 00:08:56 Now, we first replace the [at] with the @, using the replace method of strings. 71 00:08:57 --> 00:09:04 so type email is equal to email.replace 72 00:09:05 --> 00:09:13 then in brackets double quotes @ at square brackets comma then again in square bracket 73 00:09:14 --> 00:09:21 Now, type print email to get the output. 74 00:09:22 --> 00:09:29 So pause the video here, try out the following exercise and resume the video. 75 00:09:30 --> 00:09:37 Replace the [dot] with . in email 76 00:09:38 --> 00:09:42 Switch to the terminal for solution 77 00:09:43 --> 00:10:01 Type email is equal to email.replace within brackets in double quotes dot square brackets 78 00:10:02 --> 00:10:06 then again in double quotes a dot. 79 00:10:07 --> 00:10:22 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. 80 00:10:23 --> 00:10:29 So type email underscore list 81 00:10:30 --> 00:10:41 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 82 00:10:42 --> 00:11:12 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 83 00:11:13 --> 00:11:14 then hit enter. 84 00:11:15 --> 00:11:21 then print email underscore str for output. 85 00:11:22 --> 00:11:27 so type print email underscore str 86 00:11:28 --> 00:11:33 So now, notice that the email ids are joined by a comma followed by a space. 87 00:11:34 --> 00:11:40 So now pause the video here, try out the following exercise and resume the video. 88 00:11:41 --> 00:11:50 For the email underscore str that we generated, change the separator to be a semicolon instead of a comma. 89 00:11:51 --> 00:11:55 Then for that now switch to the terminal for solution. 90 00:11:56 --> 00:12:13 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. 91 00:12:14 --> 00:12:25 Then type print email underscore str. 92 00:12:26 --> 00:12:31 We see that the email ids are joined by a semicolon followed by a space. 93 00:12:32 --> 00:12:34 This brings us to the end of this tutorial. 94 00:12:35 --> 00:12:40 In this tutorial, we have learnt to, Obtain sub-strings and reverse of strings by using the index numbers 95 00:12:41 --> 00:12:56 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 96 00:12:57 --> 00:13:00 Here are some self assessment questions for you to solve 97 00:13:01 --> 00:13:08 1. Given a string s = "this is a string", how will you change it to "this isn't a list"? 98 00:13:09 --> 00:13:25 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. 99 00:13:26 --> 00:13:27 And the answers, 100 00:13:28 --> 00:13:30 1. We will use the replace function to accomplish this. 101 00:13:31 --> 00:13:39 so type s = s dot replace within brackets and double quotes string,again brackets and double quotes list. 102 00:13:40 --> 00:13:50 then s = s dot replace within brackets and double quotes is, again brackets and double quotes isn't 103 00:13:51 --> 00:13:58 We notice that every 'is' in the statement has been replaced by isn't. 104 00:13:59 --> 00:14:04 2. In order to change the string to lower case, we use the method lower() 105 00:14:05 --> 00:14:12 So type s in square brackets colon colon 2 dot lower closing brackets. 106 00:14:13 --> 00:14:15 So we hope you have enjoyed this tutorial and found it useful. 107 00:14:16 --> 00:14:21 Thank you!