 Hey y'all, I'm Mr. Gibson here and today we're going to be looking at how to use string operations in Python as you can see I've created a new notebook file here Created a couple of variables to get things started. So I've created the variable first name using our snake case So underscore between the first two words there and I've assigned that the string object Abraham and then I've created another variable last name and assign that the string object Lincoln and then a third variable named space which contains The empty string. Well, actually it's not empty. It contains a single space. You can tell by the space between the two quotes We're going to be using these three strings here today To practice some of our string operations and slicing and methods to get a feel for how we can use strings in Python since strings are going to be what contain all of our plain text and cipher text We're going to use them quite a bit. So let's go ahead and run this cell I'm going to press shift enter to run and create a new empty cell underneath it And it should load all of those into memory One thing to notice is that none of those were actually printed out to the screen. There are no calculations done So unlike when we do operations with numbers in Python when you assign values to a variable None of that gets printed to the screen. So if I wanted to print some output, I would need to use a print function So let's go ahead and create a new variable called full name And we'll create this variable by concatenating the three strings that we just created in the cell above and We'll do that by doing first name and the plus symbol, which is the concatenation symbol for strings space and then the plus symbol and then last name and When we run that cell again, we won't see anything created or printed to the screen The cell just runs without any kind of fanfare. It just does this thing But if you wanted to see what that string was, that's where our print function comes in So we can say print full name and we'll see that a new string was created That's the combination of the concatenation of those first three Strings that we started with We could now work with that full name string and do a couple of different things We could look at that name written all in uppercase if you want to use the dot upper method You can see methods in Python and our notebook environment are going to be highlighted in blue Functions will be highlighted in green strings are kind of this reddish brown color Operators are purple. So everything kind of gets a little color code to have you keep track of what's a special word Python so when I run this print command, we should see the exact same string as before but with all of the characters uppercase And there we go We saw quite a bit of different methods that strings can use so every string object has access to these methods that can Modify the way that things get printed so we could make everything lowercase using the lower method we could use the length function, which is not a method but We can figure out how long this name is including the space so the space gets counted in there So we were to say Assign to the variable length The length of the full name and then let's print out that length You can see that there's 15 characters in the string full name And at this point we've created quite a bit of variables We have the first three strings that we started with We created the full name and now we just created the variable length which is holding a number So if you ever get kind of lost in how many variables and which ones they are and what they're holding There's a nice command in Python that you can use WHOS and when you run that it'll show you all the variables that are currently stored into memory and What type they are so you can see these first three are strings as an integer and then it shows you what's currently Stored to those variables. It's a nice way if you're kind of bug checking some code that isn't working in the way That you thought it should Just run the who's command and see what's currently in memory If you watch the last video, you saw the bit about restarting the kernel This is what restarting the kernel will flush out It'll basically erase all of these variables that are currently stored and reset them It's just a really common way to avoid some bugs from popping up So now we've got these variables in our memory. Let's let's continue to work with our strings Let's take that full name string and Let's slice out some characters. So maybe I only want the first three letters. We can do that by printing out Full name we use the square brackets You'll notice that anytime you open up parentheses or brackets and the Jupyter notebook It'll try and auto-complete that for you. So you don't forget to close the parentheses or bracket later And now I'll put in some indices that I want to pull out or slice from the string full name So if I said I wanted the first three characters, that means I want the characters that are found starting at index zero and up to but not including index Three and when I run this we should see the characters that are found at index zero one and two It should be a B of R. There they are And if I wanted to go through the full name But only printing every other character starting at the first character I could do colon colon two number string slicing can take up to three Indices to work with so the one that would show up first is the starting index And if you omit that it will assume the started index zero the beginning The second index that you can provide will tell it where to stop and if you omit that it'll assume Just go right on to the end and the last index that you can provide Tells it how many to count by so if you start at the beginning count basically every two Indices and kind of collect those and print of those out and That's not really a word, but we get the idea that it's pulled every other letter starting at the first one String slicing is really powerful. We can use this to extract a lot of information from strings and just the pieces that we need So it's definitely something you want to get comfortable using in this course We could of course pull just Individual characters if we just give it a single number in those brackets so I could say first Name zero plus last name zero and that'll pull the Characters that index zero to the first character out at each of those strings and then concatenate them essentially creating in this context The initials of the person and don't forget you can use negative indices We're kind of working your way from the back of the string So if I take our full name and then reference index negative one that'll pull the character That's in the last index and display that to the screen a lot of things that we can do with strings And this is just kind of the tip of the iceberg So make sure that you read up on all of the different string functions and methods that are available to you and Python And we'll start using more of these later on in the course One last important method. I want to make sure that we we touch on today is the find method This will be the kind of foundation on how we will start converting letters into numerical representations in our next couple lessons So we wanted to find a letter in a string We could do that by taking the string name So we'll do full name and we'll end with dot find if you think about okay Well, where do I what letter do I want to find we're working with the name Abraham Lincoln? Let's find where that capital L is So if I give it this capital L string you call that a substring because we expect that it's contained in the larger string When we run this it'll tell us at which index in the full name string That we're gonna find this substring we happen to be just looking for a single Character but that substring could be as long as you want it to be We run that it's telling us that the capital L is found at index eight. So the ninth character in that string One thing to point out is that the find Method is case sensitive if you did lowercase l it's not going to find that it's going to find the one that's at index 13 And if it can't find something say we ask for it to find the letter q It returns a negative one. So you're not going to get an error message It's going to be happy to just keep on going Along its way, but it you can know to look out for that negative one If you were to use the index method, which works almost the exact same way When it doesn't find the character it throws an error for you Some people like an error message to pop up to know that things went wrong other people's like the negative one So just things keep moving through it's kind of a personal preference that each have their pros and cons So figure out what which which one works best for you And that should do it for this lesson Make sure you review all of the different string functions and methods and we'll build on that and continue to learn more of them throughout the course That's it for today. Thanks for watching catch you on the next one