 Hey everybody, this is Brian and welcome to the third tutorial with Python. We're going to cover string processing. So let's actually close these other tutorials that I've done. I think this is going to be the last one for tonight. I had steak for dinner and I'm starting to go into a food coma here, so video three is going to be the very descriptive name of this thing. And we're just going to call this super awesome time with strings. So I'm just going to make a variable and we're going to say hello world and because I really detest the hello world tutorials, we're going to just chop this thing up and have all sorts of fun with it. So we know what a variable is. We understand that a variable is something that will change. We understand that things in Python are objects and with objects we can do things. And that's what this tutorial is really about is doing things with those objects that we create. Does that sound confusing? It is kind of confusing and that's why I hated computer science classes, but it's actually a very simple concept. So the first thing we're going to do is we're going to print the length of a string. And my super awesome spelling ability, we're going to print and we're going to say string is percent d bytes long. And you remember this from our last tutorial. Now we're just going to say len, well maybe len str and str is our variable. So we're calling the len function. We'll cover functions in future tutorials, but a function is just a collection of code that runs inside of a scope. Super confusing I know, but it'll be very clear once we get there. And we're going to just print this out. So let's run this. The string is 11 bytes long. Now if you want to pause the video and count that, I can guarantee you it's going to be 11 bytes. Don't include the quotes. Now we're going to make that uppercase. Now one thing you should note, when I hit that dot or the period, you'll see it gives us a list of things. This is why an IDE is super helpful, especially when you're learning. Because you don't know all these things and you don't want to spend hours memorizing the help document and stuff, you can just hit the period and it says, oh, these are all the things you can do. Capitalize, count, and code ends with. And a lot of times it'll have a very descriptive identifier next to it. So in this case we're just going to say upper, and we're going to make it lower case. We're going to say print lower. Now you know this little self here. What does self mean? We're going to cover that in a future tutorial, but a self is a reference to the object you're currently in if you're in a class. We're not in a class, so it just ignores it. We're going to run this. You're going to see, sure enough, the string is 11 bytes long, hello world capitalized, hello world lower case. And we are going to, let's have some fun with this. We're going to find the position of a letter. Actually, let's change that to index. We're going to find the index of a letter. Now what do I mean by index? Think of this as a list. There is 11 bytes in here. The list is zero base, meaning this is position zero. I don't know if this is going to show up in the video. Then one, two, three, four, five, six, seven, eight, nine, ten, eleven. Because it's zero, if we call zero, we're going to get the letter H. If we call one, we're going to get the letter E. We don't know where that is, and we want to find it. So let's say we want to find the position of the first O. We're going to print the position of O is, and we're going to percent D here, because it's going to return a number. And we're going to say str index. Now what does that do? Let's run this and find out. The position of O is four. Well, if we count this out, zero, one, two, three, four. That's where O is right there. It's the fourth character. I should say it's the, you know, the fifth character on the fourth position because it's the zero-based index. I want to cover that one more time. Zero-based index, meaning everything starts with zero. Seems like a foreign concept. You think the first one would be one, but no, it's actually zero. Super confusing. It's one of those things you just got to learn to live with. Another thing you got to learn to live with is, let's say you do a capital O and you run this. You'll see you get an error. Trace back. Recent call. Blah, blah, blah, the code value error substring not found. It couldn't find O because it is, you guessed it, case sensitive. So we're going to actually change this a little bit. String that upper index and we're going to say O. What we're doing here is we're taking the string, making it uppercase, which we've already done, and then finding the position of the letter O. And this time we run and you guessed it, it's at position four. Two very important concepts there. Zero-based and case sensitive. Those are two things that I guarantee will trip you up as your career in a Python programming. All right, now we're going to count the number of L's. Yeah, let's just put that in there. That way it's more descriptive. There are percent D letter L in the variable. We'll say in percent S. And all right, this is what we're going to do here. We're going to make a tuple and we're going to say str.upper.count. And you guessed it, we also need to give it the variable. Because what we're doing here is we're going to say there are however many letter L's in the, and then we're just going to print out a string. There are three letter L's in Hello World. This might look confusing and I want to explain this a little bit just in case you're sitting there scratching your head. What we're doing is we're string formatting like we've done in the previous examples. And we're making a tuple. Remember a tuple is just, it's a list of objects. Tuple and list are not used interchangeably. We will cover this in a future tutorial. The difference is a list can be modified, a tuple cannot. Now, the first item in the tuple, think of it as like a book. You're on page one. The first thing is, well, we're taking the string, making it uppercase because remember searching is case sensitive and we're counting the number of letter L's. Our number of letter L's, boy I am tired. We're counting the number of L's. The second one in our tuple is just the variable itself because we're going to print that out here. That's how we get there are three letter L's in Hello World. Now, we're going to do some slicing. What is slicing? Think of this string as a loaf of bread and you want to slice this into certain chunks. Like you want half the loaf or a third of the loaf or just one little slice. We're going to print, S-T-E-R and then we're going to do these brackets here and we're going to say three. Looks very foreign to us. What does this mean? We're taking the string and then on position three we're going to print that out, which should be that L, that first L. So let's run this. Sure enough, there's the L right there. So we're taking a slice. We're getting just one little slice and then we're going to just play around with this and just get really insane with it. We're going to say we want one to four. What this means is we're going to start at the first position and we want four characters. Let's run that. Or I'm sorry, we want to go to the fourth position there. No, yeah, we did. Let's go to six just for more descriptive purposes. Yeah, pretty simple, pretty easy to understand. Now we're going to go print and we're going to go, let's go to zero position. Actually, let's do this. Let's go one, meaning we're going to start at the first. Remember this is zero base, so we're skipping this H here. We're starting at position one, which is that E. And we're going to go to the length of the string. So we're going to read the entire string. Now I'll let you pause the video and take a wild guess what this is going to do. If you said it was going to print the entire thing minus the first character, give yourself a gold star, because that's exactly what it did. Now, whoops, we want to split this. Splitting a string is very handy. Like let's say someone gives you a name and you want to split it into two variables. So we'll say name equal and we're going to make a new variable here, Brian Cairns, and just type your name. You don't have to use mine. And we're going to print and we're going to say str split. Oops, and we're going to split based on spaces. So what this is going to do is it's going to actually create a new object. I believe it's going to create a list and it's going to use the space as a splitting point, meaning if it's not a space, it becomes its own unique object. Whoops, help if I did the right variable. There we go. So you can see now we have a list with Brian and Cairns inside of it. To kind of solidify that, let's say my list equal name split and we're going to split on the space. And let's actually just print out my list. It's going to print the same thing. But we're going to say print and we're going to get just a little crazy here. My first name is %s and my last name is, you guessed it, %s. And we're going to format that. Actually, let's, I've never tried this. So this may explode drastically, but let's try this and see what happens. Oh, yeah. Not enough arguments. So we're going to actually do this. So what we're doing here is we're creating this list, which creates two unique variables because we're splitting the string into two different things. Remember, the split point is the character we defined. In this case, it's a space. We're going to print that out. And then we're going to print my first name is and then whatever the first item in the list is. And my last name is whatever the second item in the list is. Remember, this is zero base. So the first one's always zero and the second one's always one. Super confusing, I know. Drives people just insane. And sure enough, my first name is Brian. My last name is Cairns. Whew, that is a lot of work. And if we were doing a different language, like say C++ or Java, that would be probably about four or five different tutorials and we'd be about two hours in discussion. This is what I mean by Python comes with batteries included. It's very powerful and you can do some pretty complex things very quickly. Now, don't beat yourself up if you're having a hard time wrapping your head around lists and things like that because we just simply haven't talked about them yet. But we're gonna get to them very soon. That's all for this tutorial. Thank you for watching. I hope you found this educational and entertaining. The source code for this and other tutorials will be out on my website voidrealms.com. And I know I'm kind of beating a dead horse here, but be sure to join the Facebook group called Void Realms. There's like 200 programmers in there and I see a lot of people coordinating and helping each other out and it's a lot easier than trying to get a hold of me.