2.7 Manipulate strings - Video Tutorials & Practice Problems
Video duration:
13m
Play a video:
Video transcript
<v ->So, now that we've looked at functions,</v> well what are methods? So, first I want to show you an example of some methods, by looking at what we can do to strings. So I'm gonna go back to the Python Console and create a new string. Let's call it greeting. Equals hello there. Okay. And, I can call greeting again and get the value. And then, if I do a dot, well now Python gives me all of these other functions to call which is crazy. So one is capitalize, well let's see what happens when we do that. So it looks like it's a function because we have the parentheses which means that it's calling something. And then, nothing changes, but that might be because it's already capitalized. So let's look at greeting, and then, let's see, there's one called upper. And now it makes it all uppercase. And then there's also one called lower, and it makes it all lowercase. We can also do this title and then it capitalizes each first letter of a word. So what are methods? Well, they're like functions but they're associated with an object. And they need to have a reference to that object. So for example, if you have an orchestra, the orchestra knows how to play it's string section, and play it's wind section. But you would be the conductor who would tell it to do those things. You're telling an object, do this function now that you know how to do, that you have ownership of. So, similarly, strings, all have built in functions that are called methods. And so, on a string you can call this method of upper and it will take itself and then make it all uppercase. So this is different from the other functions that we've seen, like print because it doesn't really belong to any object, it's just printing things to the console. So let's look at some other methods we can do on strings. So in addition to these ones, we can also, let's see, format. So format's a good one. Or actually let's look at count first. Count will count how many instances there are of a certain thing. So how many L's are there? There's two. How many H's are there? Oh, there's just one. Because one is capitalized and one is lower case. So if I do capital H, that also gives me one. If I do lower case H count, but I do it after calling greeting lower, it now returns two. Because greeting lower will return this string and then, I can can then call count on this string and now there are two lower case H's. I can also find the location of a letter, of a character. Or even like a bunch of characters. So let's see, E, returns one, because greeting, and you'll notice that none of these things change greeting itself, it's just returning a new string. So, you'll see that greeting has an E in it, is found at index one. So H would be index zero, and E would be index one. If I tried to find the space, I would find it at five. And if I tried to find X, I would find it at negative one, which means that it didn't find it. I told you earlier that there were easier ways of inserting numbers or other variables into a string than using concatenation through the plus symbol. And so let's look at some of those now. Say I had, I wanted to specify an animal we'll use donkey. And then we'll give it a sound and donkey's say hee haw. So now if I want to say the donkey says hee haw I can create a new string. So the original way we would do it would be, the, (keyboard clicking) plus animal, (keyboard clicking) plus says (keyboard clicking) and then plus sound. So that's great. We can do sound upper, maybe that is more accurate. But there are different ways of doing this. So this way that I'm gonna show you, works in Python 2.7 as well as Python 3. So the and then some curly braces says and then more curly braces. So this is a little confusing but it's saying this is a string, we've got some stuff that's gonna be inserted in here. And it's gonna do that when we call format. So format is a method that works on a string will take each instance of these curly braces and then pass a value into them. So in this case, we're gonna pass in, oh no, we're gonna pass in animal and sound. So that works. So animal is the first value, it's gonna be put into the first instance of this curly braces and then sound gets passed into the second. And then we can do this if we want to. So, other things we can do if we wanted to change the ordering of this, we could say that this is index zero, this is index one. But we'll switch the order by actually giving an index here. And now it's reversed. Of course, if you like, there are easy ways for this to not work. If we pass in two, that's out of range, there is no two value. And then another way we can do this is specify that this is animal and this is sound. And it won't work as is, but we can (keyboard clicking) say animal equals animal and sound equals sound. So, these don't have to be the same. This has to be the same as something that's in here. So let's give it a different name to make it more clear. Maybe just A, and S. (keyboard clicking) So these are called keywords or keyword arguments. That are getting passed into this method here and the keyword of A gets the value of animal and then A can be in here, S can be in here. I can also change this. So instead of referring to the value that's passed in by the index when we did zero and one we can now use a name. Okay, and then, lastly, this is still quite verbose and so one of the big changes in Python 3.6 that was introduced is something called F Strings. And these are the kinds of strings I'm gonna be using from now on in the course because they are much simpler. So let's just get rid of all of this and then just stick an F in front of here. You can see that the color has changed and now it says the same thing. So easy, right? So how F Strings work are it will start with an F and you can have either quotes and anything that isn't surrounded by by brackets, parentheses, sorry curly braces will be treated as a string. And then anything inside of parentheses can reference a variable that already exists. So here we have animal, greeting and sound. So now, maybe we'll just say the animal says greeting. And now the donkey says hello there. And let's lower case this. Great. And then I can change this. (keyboard clicking) So now the animal says sound will produce that and we can make that upper. And that's how you can, one of the ways that you can format strings and it's my preferred way. And you can only do it in Python 3.4 or above. So that's pretty much all I have to say for manipulating strings using string methods. But one thing I did want to point out was the similarities and syntax with methods and libraries. So if you remember, we could import math and then, you know, call math.sin (keyboard clicking) for example. Of 0.5. then I'll run this. And math.sin is very similar to print maybe I'll get another string, string equals hello. (keyboard clicking) So string.upper. I'm just not passing any value in here but they're pretty much doing the same thing. It's saying from this object, in this case it's a string, in this case it's a module, get the thing called sin or the thing called upper and then call it. With these parentheses. So I would like you to notice just this pattern that it doesn't really matter if we're calling it on a string or on a module. This dot, I guess really, in Python symbolizes that this name on the right belongs to this name on the left. So from here, get this thing. From here, do this thing. Yeah, and that's methods.