 Hello everyone. In this video I want to quickly go over some very useful string functions in Python. You will be using these functions a lot because we will be dealing with data read out of files as strings throughout the semester. So good to get yourself familiar. This is a refresher video. If you need more info please get in touch. Check out some of the coding practice problems under the first week's module. Alright so let's get into it. As you know I'm going to start up here. This is the list of essential functions we're going to quickly go through. So I am going to create a string. I'm here in handy dandy pie charm. I'm going to create a string and assign it to a variable. CSE 231 introduction to data structures. Okay that's the course. So now I've got this string oops inside my variable and if I print out my variable I should see the value right. So another reminder to run this just click right click on the file click run strings or use one of the keyboard shortcuts. Okay so now what are some of the functions that are useful? Well we can turn the string into all uppercase by using this upper method. Okay so right now this is kind of magic mojo this weird kind of syntax. I'm sure you've seen it before. This is technically called a method on the string class. It looks a lot like a function and it is. But the distinction is I've got a variable and more specifically I've got a string that's living in here and I'm calling dot upper on that string. Okay that's what makes this a method versus a function. We will get into methods versus functions later. But right now just know that this is an operation that you can call using this syntax on a string or a string variable and do some manipulation on it. So if we print this we will get csc231 introduction to data structures all uppercase. We can also lower case the string. So I've done a little trick here in PyCharm. I have duplicated the line you can do this with control D on Windows or command D on Mac. So I've duplicated the line where my cursor was and I'm going to just change upper to lower and let's do one more which is title, title case. And we're printing out all of these. So here I've got the original. Here I've got the uppercase. Lower case we have lower case all of the alphabetic characters and then this is title case. So there's some subtle difference. I'm uppercasing the first letter of each word. Just useful little changes. Now what's important here though, something you absolutely have to remember, we are not actually changing the value, the string that is kept inside this course variable. In fact, if we print out course right here after doing all of these things, you will see the original string from up here. Watch. See? Last one. This is the original string from up here at the top. That is because strings are immutable. Once you create one and right up here I created one, you can't actually change it. What I'm doing here is I am getting a copy of this string back and that copy has been changed. So it's returning the value of it. And I am printing that value that's coming back out of here. So once again, this returns a value and then I am printing that value. I could save that value. So let's maybe save this value. Uppercase gets this. So now if I print uppercase, it'll come out. Here it is. So course.upper returns a value and then it's up to you to do something with it. Here I'm assigning it to a variable. In these cases, I'm just printing out that return value. So let's move on. I'm going to comment these lines out just to kind of give myself some breathing room down here. The LEN function. How many characters are in the string? Well, in this case, all it's going to do is it's going to count the total quantity of characters. I don't know what that is. Forty-some. Forty-one. There you go. Useful. You sometimes need to know the length of strings and other things. Is something in a character, in a string. Very useful. You can use this in Boolean expressions. So for example, if the string csc is in course, print, this is a computer science course. Something like that. Else, print, not computer science. This is a computer science course. Why? Because csc in course returns true. And all it's doing is it's scanning through the string here and seeing if it can match this string. It doesn't have to be a string. It can be a single character. It can be a lot of different things. But since we're dealing with strings, let's keep it to characters. Is a character or is a string in the string? All right. Find x. Find the first occurrence of x in the string and return its index. Okay. So what is the index of a string? The index is where a character appears. So for example, down here in my course, let's say I want to find the first occurrence of the character 2. That's going to be at index 0, 1, 2, or this is 2. The blank space is a character. It's at index 3. So the first occurrence of 2 in the string is going to be at index 4. Okay. So let's try that. Print course.find the first instance of the character 2. There it says it's at index 4. Let's maybe use an f string to make this a little more useful. The first occurrence of the character 2 is at index. And do you remember from your f string video, you can use these squigglies and you can put an expression in there. Course.find the character 2. All right. So this will get evaluated. And whenever it returns, we'll get converted into a string and put here. The first occurrence of the character 2 is at index 4. Okay. So we're searching for the character 2. We can also replace. We can replace all occurrences of something in a string. So for example, maybe we want to get rid of, and this is something you may find useful in this class. Suppose we want to get rid of all the o's, the character o, and replace it with something silly like x, y, z. We can do that with this replace method. CSE 2, 3, 1, interest goes to data structures. But this isn't just silliness. We can do useful things too. Like clean gets course.replace. Let's get rid of, say, all the spaces in the name. So let's replace all the spaces. I got a space there with nothing. Empties. Nothing in here. Just two apostrophes. Let's print out clean. CSE 2, 3, 1, introduction to data structures. Kind of neat. Kind of clean this up a little bit. So replace. Replace all occurrences of the string x with the string y inside this string. Alright. What else can we do here? Starts with and ends with. Okay. So these will return a Boolean function. So we can kind of, let's do something similar to what we did before. If course dot starts with CSE print, it's a computer science course. Else print, it's not a CSE course. It's a computer science course, right? So starts with is going to look at the first characters of the string, CSE. And it's going to try and match this whole other string to it. And if that returns true, hey, it'll return true or false. Okay. Let's return, let's make this something else. If course dot starts with MAT, it's a math course. Otherwise, it's not a math course. Okay. So this should return false because our course, its value is CSE 231, Introduction to Data Structures. Okay. It's not a math course. And again, it starts with. So if we change this to 231, even though 231 is in here, it's not going to find it. Right. This is going to return false. Okay. Cool. Now the last two that I want to talk about are going to be really important and useful for you in this course. Okay. So those are the split function and the strip function. So what split does is it takes this string and splits it on whatever string you give it. Okay. What it returns is a list of everything that's not this character. Okay. So for example, let's split. We'll call it parts. Okay. And let's do course dot split on this guy, the little hyphen right here. So what this should do, it's going to find all of the hyphens. There's only one in this particular string. And it's going to return the parts to the left of it and the parts to the right of it. Okay. As a list. So let's print the parts. Alright. So I've printed my parts. You'll notice the thing I split on is gone. This is a list. Right. The lists in Python start with brackets. Here's the first list element, this string. There's a little comma. And then here's the second list element, this string. Okay. Now we've got a list in parts. And we can do things like loop over it for P in parts and print the individual elements. CSE 231 introduction to data structures. Okay. Let's come back to this in a second. I'm going to comment this out for now. And let's do something else. Another example. Let's do the words. Let's just get the individual words out of here. Right. So in other words, let's say everything that's not a white space. Words gets course.split. And let's split on the space character. Okay. So there's a space character in here. And if I print words, we will see it is a list. CSE 231 the hyphen introduction to data structures. So I've gotten a list back here with one, two, three, four, five, six, seven elements in it. Okay. And now again I can do useful things with this list like four word in words. For each word in words, let's print it out. What do we got? CSE 231 introduction to data structures. One line each. Okay. Pretty nifty, right? We will wind up using this split function quite a bit. We'll see it actually in the first assignment as well. Very useful. Very useful if you like want to deal with the individual elements here, right? Maybe I want to print the length of each word. I can do that now, right? So very useful function or method excuse me. All right, let's go back to what I had before for a moment to illustrate our last function, which is the strip function. Okay. So what strip does is it removes, actually let me get this out of here, it removes leading and trailing white space from a string. So let me take my previous splitting example. I split on this, the hyphen here to get the words. And there's actually some white space here, right? When I print it, see how introduction to data structures is over a little bit. And there's actually some white space at the end of the 231. The reason for that is I've got the space in here, right? From splitting. Maybe I want to get rid of that. Maybe I want to clean it up. Usually when you're processing strings and data, you do not care about leading and trailing white spaces. You've got to get rid of it. But it tends to show up, especially when you're reading from, say, a file. And I'm going to be giving you throughout the semester, big files of data. And it's going to be string data, lines of string data. And you have to read it in, put them in the list, and do things to them. Well, you want to get rid of the leading in the trailing white spaces, because if you're going to be comparing things, you don't care about those leading and trailing white spaces usually. There's an easy way to get rid of them, okay? And that's to use this strip function. Okay, so, what I'm going to do, now, this loop, it's printing out each element of this list, including the white space. But if I strip these strings, it gets rid of this extra white space. See that? Okay. Let's see, maybe just a simpler example. Let's make a nonsense string. And I'll put some spaces and maybe a tab or three. And my name is mud, right, or mood. Maybe a couple more. Something bizarre has happened here, right? And let me print this out. Okay, my name is mood. I've got some extra white space here, and of the line. All right, all this extra stuff. Let me strip this and print it. Okay. So it's removed from my name is mood, or from this nonsense line, all the beginning, and there's no trailing white space. And that's okay, but it's removed all the beginning white space. The white space in the middle, it is kept. That's a good thing, okay? If we want to get rid of that, we've got to replace it using the replace function. We could do that if we want to. But it's all gone, okay? But again, remember, none of these functions actually change the method, the original string itself. I'm printing out the return value of the stripping. Okay? When I print nonsense again, it's still this original thing because strings are immutable. If I want to change it permanently, I have to do something like this. Nonsense gets nonsense dot strip. This reassigns the nonsense variable to be the result of this guy. Okay? So now when I run it, I get this, right? So these are all string functions I expect you to be familiar with. You will probably be using them at some point in the semester, we will get time to practice them as you go along. But just beware, they're coming down the pipe, start to learn them now, start to get familiar with them now, start to practice them now. And as always, if you have any questions, please let me know.