1 00:00:00,460 --> 00:00:07,370 Cheers and welcome back to another class of our course about the complete introduction to Python. 2 00:00:07,400 --> 00:00:14,910 So in this class we are gonna talk about a very important thing which are strings and we are gonna talk 3 00:00:14,910 --> 00:00:19,330 about different manipulations that you guys can do with the well with strings. 4 00:00:19,330 --> 00:00:26,010 So as we talked in our last class strings are simply text that you want when if you want to add text 5 00:00:26,010 --> 00:00:28,980 to your code to when you are programming. 6 00:00:28,980 --> 00:00:35,460 Strings are simply a way that you can add the text on your let's say on your code when you are programming 7 00:00:35,460 --> 00:00:36,520 something. 8 00:00:36,840 --> 00:00:41,820 And this is why it is very important to understand how we can work with them what exactly we can do 9 00:00:41,910 --> 00:00:47,400 with them and what are the different manipulations that we can do with them. 10 00:00:47,400 --> 00:00:49,800 So in this course how are divided all this. 11 00:00:49,800 --> 00:00:55,830 We are gonna talk about the first thing storing data in our variables. 12 00:00:55,890 --> 00:01:01,650 So storing strings in our variables we are gonna talk about the string storage after that. 13 00:01:01,870 --> 00:01:06,660 Well will talk about the storage and how we can manipulate it after what we are going to talk about 14 00:01:06,660 --> 00:01:07,130 functions. 15 00:01:07,130 --> 00:01:09,540 So what is some different functions. 16 00:01:09,540 --> 00:01:15,300 There are hundreds of functions that exist for strings but we are going to talk about the most common 17 00:01:15,300 --> 00:01:15,520 ones. 18 00:01:15,520 --> 00:01:18,370 So in this case if I'm not mistaken we have four of them. 19 00:01:18,540 --> 00:01:23,450 And finally we are gonna talk about a small little thing that you guys can do. 20 00:01:23,670 --> 00:01:26,040 Once again you'll see this by the end of this class. 21 00:01:27,000 --> 00:01:30,510 So my goal right now is simply to present you what you can do with strings. 22 00:01:30,540 --> 00:01:35,790 Once again it's going to be some basic manipulations but you guys will see it's gonna be really cool. 23 00:01:35,790 --> 00:01:36,610 So let's start. 24 00:01:36,750 --> 00:01:40,950 So the first thing I'll ask you guys to do is simply open up your by charm or your. 25 00:01:41,010 --> 00:01:47,050 Any text editor that you guys use to be able to run Python when it's all done. 26 00:01:47,130 --> 00:01:52,960 The first thing we will write down is simply a small sentence or in this case will write down print. 27 00:01:53,070 --> 00:01:55,590 Let's say I love pizza. 28 00:01:57,330 --> 00:01:58,240 So we will print. 29 00:01:58,260 --> 00:01:59,010 I love pizza 30 00:02:03,220 --> 00:02:06,060 in our goal right now. 31 00:02:06,070 --> 00:02:13,470 Simple simply just run it so you can see we have our way hour our string which is I love pizza and we 32 00:02:13,470 --> 00:02:18,610 have our small app that is printed right here so our app we asked Python to print. 33 00:02:18,660 --> 00:02:24,890 I love pizza but well you can also do we you can store this instead of printing. 34 00:02:24,900 --> 00:02:25,700 I love pizza. 35 00:02:25,710 --> 00:02:31,470 We can store this string into a variable so let's say for example we want to create a variable that 36 00:02:31,560 --> 00:02:36,360 we will name sentence and we want to store it in it. 37 00:02:36,360 --> 00:02:38,820 I love pizza so we store. 38 00:02:38,820 --> 00:02:44,010 I love pizza all right. 39 00:02:44,010 --> 00:02:47,340 And we can run it so we can see we have I love pizza first. 40 00:02:47,340 --> 00:02:54,780 So just create our second function so in this case we want to run. 41 00:02:55,260 --> 00:02:55,660 All right. 42 00:02:56,580 --> 00:03:01,470 So as you can see in the first case scenario we print I love pizza and we run. 43 00:03:01,470 --> 00:03:11,430 I love pizza so the app is simply asking Python to run this text the second so in the second thing right 44 00:03:11,430 --> 00:03:11,610 here. 45 00:03:11,610 --> 00:03:14,240 So if we run it in both cases it's reading. 46 00:03:14,280 --> 00:03:14,900 I love pizza. 47 00:03:15,180 --> 00:03:22,070 But in the second part right here what we ask Python to do is add first name sentence. 48 00:03:22,080 --> 00:03:26,340 I love pizza so we going to store in the variable sentence the data. 49 00:03:26,340 --> 00:03:33,050 I love pizza and then we ask Python to run this variable which refers to I love pizza. 50 00:03:33,060 --> 00:03:35,950 So you get the difference. 51 00:03:36,030 --> 00:03:42,540 So instead of writing down the whole thing each and every time we can simply store it into a variable 52 00:03:42,870 --> 00:03:47,030 and ask Python to run it right after that. 53 00:03:47,040 --> 00:03:47,340 All right. 54 00:03:47,670 --> 00:03:53,390 So we'll just delete the first part of the code when it's all done. 55 00:03:53,460 --> 00:03:57,180 The next thing that we will talk about so right now you understand the data storage. 56 00:03:57,180 --> 00:04:00,500 So even if we change the I love pizza instead we write down. 57 00:04:00,510 --> 00:04:05,790 I love pizza and let's say nachos and we run our code. 58 00:04:05,790 --> 00:04:11,490 You can see that python will still print our sentence which is I love pizza and nachos. 59 00:04:11,520 --> 00:04:11,720 All right. 60 00:04:11,820 --> 00:04:17,280 So I know that you guys understand the first part which is storing data into a variable. 61 00:04:17,280 --> 00:04:24,810 Well storing a string into a variable we're going to talk about the second part which is in this case 62 00:04:24,810 --> 00:04:32,070 functions so functions are a very important part in string manipulation and we are going to talk about 63 00:04:32,070 --> 00:04:35,720 some basic functions that we are going to use for discourse. 64 00:04:35,730 --> 00:04:37,870 So you'll see it's not that hard to understand. 65 00:04:38,010 --> 00:04:40,220 You can do some cool stuff with it. 66 00:04:40,330 --> 00:04:46,500 Right so the first thing that we will talk about will be our first function which will be the upper 67 00:04:46,500 --> 00:04:46,980 function. 68 00:04:46,980 --> 00:04:56,580 So in this case what we want to do is simply take all the characters of this will of this sentence which 69 00:04:56,580 --> 00:05:02,160 is I love the work of the string and we want to put them all into upper characters. 70 00:05:02,280 --> 00:05:03,480 So how we'll do this. 71 00:05:03,480 --> 00:05:04,770 It's very simple. 72 00:05:04,770 --> 00:05:07,560 We'll write we have right here our Well what we ask. 73 00:05:07,560 --> 00:05:10,860 Bite onto the print and we'll ask him to print sentence. 74 00:05:11,250 --> 00:05:16,240 So at the end of sentence simply click on that and we'll write down our function. 75 00:05:16,350 --> 00:05:18,640 As I said our function is the upper function. 76 00:05:18,660 --> 00:05:22,230 So you just write down upper you open and close parentheses. 77 00:05:22,410 --> 00:05:29,700 And when you run the app as you can see Python printed I love pizza and nachos in upper cases because 78 00:05:29,820 --> 00:05:35,610 we asked Python to print our our app in upper cases. 79 00:05:35,670 --> 00:05:35,970 All right. 80 00:05:36,390 --> 00:05:41,160 So when it's done the next thing that I'll ask you guys to do in Sim is simply take some letters in 81 00:05:41,420 --> 00:05:47,700 a while in complete randomness and put them in the upper characters 82 00:05:51,220 --> 00:05:55,960 when it's all done right now what we'll see we'll see that python. 83 00:05:56,050 --> 00:06:02,580 There is also a way for us to ask Python to put all this sentence in lower characters. 84 00:06:02,590 --> 00:06:04,660 In this case how this will work. 85 00:06:04,780 --> 00:06:06,970 It's simply another function that we'll use. 86 00:06:06,970 --> 00:06:08,440 So how do we read this function. 87 00:06:08,470 --> 00:06:10,440 We will write it down the exact same way. 88 00:06:11,360 --> 00:06:13,170 So let's start from the beginning. 89 00:06:13,180 --> 00:06:15,370 We have our print sentence. 90 00:06:15,370 --> 00:06:18,750 So this is the code that will ask Python to execute. 91 00:06:19,000 --> 00:06:26,530 We have our sentence we'll write down dot and then our second function will be lower so lower and you 92 00:06:26,560 --> 00:06:27,900 open and close parentheses. 93 00:06:28,000 --> 00:06:32,950 And then the next thing that you guys will do is simply run the app so you can see I love pizza and 94 00:06:32,950 --> 00:06:37,070 nachos is on in Lower cheese. 95 00:06:37,240 --> 00:06:37,570 All right. 96 00:06:37,810 --> 00:06:44,890 Amazing so right now you know how to put all the characters in your in your python string in upper or 97 00:06:44,890 --> 00:06:46,560 lower case Larry. 98 00:06:46,570 --> 00:06:53,830 Now let's say that you guys want to verify if all characters are in lower character so you can ask. 99 00:06:53,830 --> 00:06:59,470 There is a way that you can ask Python to verify this so you can ask python is all the characters right 100 00:06:59,470 --> 00:07:02,330 here are in low are lower chapters. 101 00:07:02,510 --> 00:07:08,980 So how we will do this instead of writing lower we simply will write down is lower. 102 00:07:08,980 --> 00:07:10,470 So this is showing up. 103 00:07:10,570 --> 00:07:12,430 So you simply write don't print the sentence. 104 00:07:12,430 --> 00:07:13,780 Dot is lower. 105 00:07:13,780 --> 00:07:18,330 So right now you ask Python if all those characters are in lower character. 106 00:07:18,520 --> 00:07:19,610 So we'll run the up. 107 00:07:20,680 --> 00:07:28,090 And as you can see Python returns us false which means that all the characters are not in lower chases. 108 00:07:28,150 --> 00:07:28,420 All right. 109 00:07:28,960 --> 00:07:31,030 But right now let's verify it. 110 00:07:31,030 --> 00:07:34,390 If we put all the characters in lowercase is sold. 111 00:07:34,410 --> 00:07:35,810 This is very simple. 112 00:07:35,810 --> 00:07:36,300 We'll call it. 113 00:07:36,400 --> 00:07:38,500 We'll have our right here print. 114 00:07:38,500 --> 00:07:40,090 So let's do it step by step. 115 00:07:41,590 --> 00:07:45,270 So we have our first part of the code which is print sentence. 116 00:07:45,280 --> 00:07:48,080 We ask Python to print this part of the code. 117 00:07:48,130 --> 00:07:52,320 So the first thing we'll do we'll put everything that is here in lower case is how we'll do this. 118 00:07:52,360 --> 00:07:56,500 Simply write down sentence dot lower. 119 00:07:56,500 --> 00:07:59,890 So the first part is putting everything in lower cases. 120 00:07:59,950 --> 00:08:04,280 So we have our first part of the the app. 121 00:08:04,330 --> 00:08:14,320 The second part will be asking Python if all those if all the text in this string in this variable is 122 00:08:14,320 --> 00:08:15,720 in lowercase i will do this. 123 00:08:15,760 --> 00:08:20,450 We'll simply ask him is lower and we open in close parenthesis. 124 00:08:20,470 --> 00:08:22,360 So when it's all done we run our app. 125 00:08:22,660 --> 00:08:26,810 And as you all right. 126 00:08:26,920 --> 00:08:30,630 Don't forget to add the double parenthesis they both please. 127 00:08:30,640 --> 00:08:36,210 When it's all done you can simply run your app and as you can see it returns the value. 128 00:08:36,250 --> 00:08:36,930 True. 129 00:08:37,000 --> 00:08:40,770 So very simple what you what is it what exactly happened here. 130 00:08:40,810 --> 00:08:48,290 You printed sentence then you asked Python to put everything that is in this variable in lower cases 131 00:08:48,620 --> 00:08:55,510 and then you ask python is everything in lower cases in this in this variable. 132 00:08:55,760 --> 00:08:56,360 Right. 133 00:08:56,420 --> 00:09:02,360 So this is the first the thing that we will that we saw today so right now you know how to put everything 134 00:09:02,360 --> 00:09:05,060 in lower or protractor and verify it. 135 00:09:05,570 --> 00:09:10,670 So as you can see it's very simple to do than say well the another function that we are going to talk 136 00:09:10,670 --> 00:09:17,040 about is a function to calculate the length of a certain let's say string. 137 00:09:17,090 --> 00:09:20,040 So in this case this is simply another function. 138 00:09:20,040 --> 00:09:21,680 It's very simple to use. 139 00:09:21,680 --> 00:09:27,650 So what we'll do we'll write down once again our first part of the code which is print sentence we ask 140 00:09:27,650 --> 00:09:30,410 byte on to print the variable sentence. 141 00:09:30,410 --> 00:09:32,170 So we'll just try if it works. 142 00:09:32,240 --> 00:09:33,160 Yes it's the works. 143 00:09:33,170 --> 00:09:34,010 Amazing. 144 00:09:34,010 --> 00:09:39,420 So right now what we'll do we'll work with our second segment function. 145 00:09:39,680 --> 00:09:41,170 So it's the function length. 146 00:09:41,240 --> 00:09:42,530 So how do we write it down. 147 00:09:42,530 --> 00:09:44,040 Very simple print. 148 00:09:44,150 --> 00:09:49,820 Then you write down L E N and you open up your parentage and close it. 149 00:09:49,940 --> 00:09:50,930 So let me explain. 150 00:09:51,140 --> 00:09:53,510 Let me explain to you what exactly happens here. 151 00:09:53,540 --> 00:09:56,530 So you ask Python to print. 152 00:09:56,540 --> 00:10:05,900 So you ask Python to print you right here the length of this variable so you get it first part of the 153 00:10:05,900 --> 00:10:14,420 code asking Python to return you the length of this variable in this case the length of this variable 154 00:10:14,510 --> 00:10:19,210 is twenty three because we have twenty three characters and I love pizza and naturals. 155 00:10:19,340 --> 00:10:22,500 Let's say we write down and I don't know. 156 00:10:22,580 --> 00:10:24,760 And tackles. 157 00:10:24,800 --> 00:10:32,080 So we run our code and right now we have 33 characters in our sentence. 158 00:10:32,180 --> 00:10:33,660 So you get the point. 159 00:10:33,680 --> 00:10:34,310 All right. 160 00:10:34,310 --> 00:10:38,240 Next thing that we want to do right now you know how to calculate the length. 161 00:10:38,240 --> 00:10:40,170 So very simple you print. 162 00:10:40,420 --> 00:10:41,480 I'll repeat it once again. 163 00:10:41,510 --> 00:10:47,590 So you print the length of this variable very simple to understand it even if you change the length. 164 00:10:47,600 --> 00:10:53,380 Well python will adapt and change the answer that it will give you all right. 165 00:10:53,500 --> 00:11:01,550 The next the next vote the next function that we are going to talk about sorry will be the function 166 00:11:02,270 --> 00:11:03,120 of replays. 167 00:11:03,140 --> 00:11:09,010 Let's say you want to replace a certain variable right here by something else. 168 00:11:09,020 --> 00:11:11,020 So in this case I will do this. 169 00:11:11,060 --> 00:11:12,410 It's very simple. 170 00:11:12,410 --> 00:11:15,800 So we have our first part of the code which is the string. 171 00:11:15,830 --> 00:11:23,090 So you simply write down print you open up your parenthesis and you write down a sentence which is your 172 00:11:23,090 --> 00:11:24,030 stream. 173 00:11:24,130 --> 00:11:24,410 All right. 174 00:11:24,830 --> 00:11:32,300 As I said the next the next function will be the replaced functions or simply click on dot replace and 175 00:11:32,300 --> 00:11:34,030 you open up your parentheses. 176 00:11:34,040 --> 00:11:34,380 All right. 177 00:11:34,670 --> 00:11:40,400 So the first thing we'll ask will have to do is write down the word that we want to replace. 178 00:11:40,400 --> 00:11:44,600 So in this case the word we want to replace will be pizza. 179 00:11:44,630 --> 00:11:45,900 Let's say it's pizza. 180 00:11:46,010 --> 00:11:51,560 So you open up your will to commas and you write down pizza. 181 00:11:51,590 --> 00:11:55,850 So very important you need to write down the same letters that are here. 182 00:11:55,850 --> 00:11:59,510 You can't use capital letters when it's not capital letters. 183 00:11:59,510 --> 00:12:07,430 When you choose choose the word simply click on comma and write down the word which by which you want 184 00:12:07,430 --> 00:12:08,010 to replace me. 185 00:12:08,030 --> 00:12:10,640 So it's a want to replace pizza by hamburgers 186 00:12:14,250 --> 00:12:18,400 and simply click on play. 187 00:12:18,480 --> 00:12:21,800 So as you can see right now our app will return this. 188 00:12:21,840 --> 00:12:24,660 I love hamburgers and nachos and tackles. 189 00:12:24,900 --> 00:12:30,290 Since we have our variable right here and we ask Python to print all this. 190 00:12:30,660 --> 00:12:33,000 So very simple to use as well. 191 00:12:33,000 --> 00:12:34,550 Let me explain to you one more time. 192 00:12:34,710 --> 00:12:42,580 So you ask Python to print this variable in which you will replace the word pizza by the word hamburger. 193 00:12:42,940 --> 00:12:48,020 So as you can see it's simply you are you simply talking with the computer. 194 00:12:48,030 --> 00:12:51,080 So you ask him to replace things. 195 00:12:51,210 --> 00:12:57,060 So it's very very simple to use another variable that we owe another viable so another function that 196 00:12:57,060 --> 00:12:57,750 we will talk about. 197 00:12:57,750 --> 00:13:01,830 Once again it's a very simple function to use is the index function. 198 00:13:01,920 --> 00:13:09,410 In this case if let's say we are looking for a certain character it will give us the number of the character. 199 00:13:09,420 --> 00:13:12,840 So let me just show you this real quick. 200 00:13:12,840 --> 00:13:14,890 So how will write this down. 201 00:13:14,940 --> 00:13:15,900 Very simple. 202 00:13:15,900 --> 00:13:21,420 We'll write down our first part of the function which is well the function will write down a first part 203 00:13:21,420 --> 00:13:24,270 of the code which is print sentence. 204 00:13:24,570 --> 00:13:27,910 Then we click on index. 205 00:13:28,110 --> 00:13:33,440 So the index function and let's say we are looking for the letter B. 206 00:13:34,140 --> 00:13:39,350 So in this case we're looking for the letter P and we play Oracle 207 00:13:48,660 --> 00:13:49,500 here we go. 208 00:13:49,500 --> 00:13:51,530 So as you can see it's going to be the seventh. 209 00:13:51,570 --> 00:13:53,410 It's the seventh character. 210 00:13:53,520 --> 00:13:59,280 Yes it's maybe not the seventh but when you calculate with python you start to calculate with the by 211 00:13:59,280 --> 00:14:06,000 year the values you saw in this case we have zero one two three four five six and seven. 212 00:14:06,270 --> 00:14:12,060 And yes you have to calculate the spaces also so you don't start to calculate by would 1 you start to 213 00:14:12,060 --> 00:14:13,260 calculate with 0. 214 00:14:13,260 --> 00:14:18,690 So this is the main difference between python and let's say when you calculate by yourself Python Keller 215 00:14:18,700 --> 00:14:23,590 starts by you so the eye right here will be zero. 216 00:14:23,640 --> 00:14:30,340 The space will be one the L two three four five six and the feet will be seven. 217 00:14:30,360 --> 00:14:31,310 So let's say we will. 218 00:14:31,310 --> 00:14:33,670 We are looking for the A. 219 00:14:33,750 --> 00:14:37,430 In this case the first a and we run our up. 220 00:14:37,660 --> 00:14:40,040 It's gonna be the eleventh charity. 221 00:14:40,050 --> 00:14:43,680 So if we are looking for something Python can simply well find that out. 222 00:14:44,790 --> 00:14:45,210 Let's go. 223 00:14:45,210 --> 00:14:47,680 Also is that we can look for complete words. 224 00:14:47,790 --> 00:14:49,630 Let's say we are looking for this. 225 00:14:49,640 --> 00:14:58,590 And so we just write down our weird end which is a and in the letters in the in small letters and python 226 00:14:58,590 --> 00:15:04,950 will return the value thirteen which is the value of the well will return to start the beginning of 227 00:15:05,010 --> 00:15:11,610 our let's say in this case of our end which starts at the value thirteen. 228 00:15:11,640 --> 00:15:11,930 All right. 229 00:15:12,240 --> 00:15:16,160 So after all this you can simply well you can simply delete all this. 230 00:15:16,200 --> 00:15:19,620 So another cool thing that I wanted to show you guys it's going to be the last thing that I'll show 231 00:15:19,620 --> 00:15:25,230 you is let's say for example you have a sentence and you want to print. 232 00:15:25,260 --> 00:15:28,970 Well you want to print a part of the sentence at. 233 00:15:29,300 --> 00:15:31,610 On the first line and then add the second line. 234 00:15:31,620 --> 00:15:37,650 So let's write down print and let's write down something. 235 00:15:37,710 --> 00:15:50,270 Well once again I love pizza and nachos and backwoods. 236 00:15:50,750 --> 00:15:52,340 All right. 237 00:15:52,340 --> 00:15:53,560 Perfect. 238 00:15:53,630 --> 00:15:55,790 So right now if we want to drop what. 239 00:15:55,880 --> 00:16:01,580 Well if we run all this we have our sentence which is I love pizza and nachos and tacos. 240 00:16:01,610 --> 00:16:06,760 All right let's say we want to drop the first the first part of our sentence. 241 00:16:07,010 --> 00:16:08,300 We want to drop it here. 242 00:16:08,390 --> 00:16:10,890 And the second part lower even lower. 243 00:16:11,210 --> 00:16:13,110 So we'll do this is very simple. 244 00:16:13,130 --> 00:16:15,950 You simply have to draw this line and write it in. 245 00:16:16,250 --> 00:16:18,930 So when you run your code you will have. 246 00:16:19,010 --> 00:16:22,180 I love pizza and nachos. 247 00:16:22,370 --> 00:16:24,080 So let me just read it once again. 248 00:16:24,080 --> 00:16:29,770 As you can see and let's say we want to do the exact same thing in this part of our code. 249 00:16:29,780 --> 00:16:31,840 We can do with simply like this. 250 00:16:31,850 --> 00:16:37,320 So if we run the code as you can see it's right now running on three different lines. 251 00:16:37,400 --> 00:16:43,090 So each time that you each time that you want to ask Python to run a part of your code on another line. 252 00:16:43,100 --> 00:16:44,030 It's very simple. 253 00:16:44,030 --> 00:16:47,900 You simply have to put this symbol right here and the letter. 254 00:16:47,960 --> 00:16:52,160 And so it will simply run your code on a little red line. 255 00:16:52,170 --> 00:16:52,490 Right. 256 00:16:52,490 --> 00:16:54,940 Right now you understand how you can manipulate strings. 257 00:16:55,190 --> 00:17:00,800 And while some basic string manipulations we talked about some functions right now we talked about storing 258 00:17:00,800 --> 00:17:01,660 variables. 259 00:17:01,670 --> 00:17:05,360 Well we've talked about storing string in variables and how we can use it. 260 00:17:05,810 --> 00:17:12,140 So until now you can just practice it and play with it you'll see it's very cool to use. 261 00:17:12,140 --> 00:17:17,060 There are as I explained there are hundreds of other functions that you can find on the Internet with 262 00:17:17,060 --> 00:17:19,140 strings if you guys are interested. 263 00:17:19,200 --> 00:17:28,130 I and I what I definitely suggest you to go on Google and look for those functions and you can play 264 00:17:28,130 --> 00:17:28,590 with them. 265 00:17:28,590 --> 00:17:32,820 Also they're not very hard to understand and normally they use it's easy to use. 266 00:17:33,420 --> 00:17:34,540 So that's it for this class guys. 267 00:17:34,610 --> 00:17:36,620 And see in our next class.