Hello, everyone. And welcome back to intrude a python here on cyber eri on demand, I as always I'm your instructor, Joe Perry. And if you're watching this video, it means that you have completed our lab or lab assignment. Whether you did it through next tech are spectacular partner organization or you did it through just the assigned
document. Either way, excuse me. Either way,
the important thing is that you've gotten a little bit of experience programming
gotten your hands on python really gotten to actually write some code. If you haven't done the lab yet, very highly recommend you go back and do that. I as a programmer, I never really got into program. He never really understood it until I actually started to write the code myself. And as we go through strings,
numbers, lists and dictionaries, which were these next few labs or these next few lessons Rather,
it's gonna be a lot easier for you to understand if you've gotten comfortable right in your own python code.
No further ado Lesson six objectives we're gonna learn about string methods were gonna kind of talk about what methods are. Briefly, we're gonna understand we're gonna use string slicing, and we're gonna learn to take input from the user. And then we're gonna learn to write better print statement. So this lesson is gonna be divided into two videos. The first is gonna be about string methods and strings slicing. The 2nd 1 is gonna be about the input output
functions that we just discussed.
So string methods, What our methods will you remember for our very earliest video, we talked about this dirt function and what that did is it showed us all of the objects are all the functions and all the names that have defined in our current space.
So we can use that to understand what methods are a little bit more effectively creating ourselves a variable X equals
here. You see, we have all these underscored names. We're not gonna worry about those right now, those air internal and not really for our use.
we look down here, we see these names in the list that don't have underscores and, for example, we see the ones that we're gonna I'm gonna focus on right now, lower
And what These are our methods. Methods are functions that are attached to a given object and then our intermediate advanced python classes. We spend a whole bunch of time on classes and objects and python. We talked about object orientation.
In that time, you're gonna learn under the hood a lot more about what methods and attributes are and what they do and what, therefore and all that sort of thing. For now, just understand that methods are functions which are attached to an object.
Let's clear a screen here a little bit. So remember, we have X, which is our hello world that first ever ever script we ever wrote. Hello, world. And then we're going to do x dot upper. And this is the way you use methods that are attached to a given object. If we're trying to use the upper method from X, we do x dot upper and then we call that function.
There is no arguments. The upper function. We just call it and you see that it's gonna print hello world in all caps. Now, For those of you who have never written assembly or C code, it's kind of hard to describe to you how much more work it would take to do this in those languages. I address that because, like Upper and lower were two of the first functions I ever learned in Python, and it absolutely blew
my mind. I was amazed at the idea that you get so much done so easily with these men.
So that's how you use string methods, and we're gonna talk about a few more of them. So one of the ways that you can use lower, which is the companion function to upper really effectively is if you have
a list that you're trying to compare against some input. So, for example, we gonna have this list of Joe,
and then we're going to have some input. What? We're just gonna call our input variable,
and you can see there that the input variable and normally you would get this from your user. But we haven't discussed that just yet, so I'm not gonna worry about it. The input variable doesn't exactly match the first name in this list. It is the same name, but the input has a capital letter in it.
What you can do is four I
I remember Double equal and is air similar? This is one of the cases in which you're gonna find a difference. If you're performing methods against your string, you're gonna want to use double equal instead of is. And again, the constants of identity are really part of object orientation. So we will discuss those in later classes. For now, just understand that if you're making use of a method, it is safer to use the W
If I equals input variable
Hopefully this is gonna work for us. Sure enough, it founded the variable that we're looking. Or if I'm the name we were looking for
because of the fact that input variable
now if we were just to do
for I am name list if I
equals equals input variable.
not what I meant to do.
You could see it'll never print found it because of the fact that those two names don't exactly match. That's the concept of string methods. There are a whole bunch of them here. As I said, you can find them by doing your ex. I highly recommend you play around with them things like is decimal or is digit. You can find a lot of useful information just by messing with those methods.
Now, however, we're going to move on to the next concept. And for that, we're gonna have our interests. Are first string here Hello, world,
And what we're talking about now is called strange slicing or string indexing. Now you may remember
from very briefly when we're talking about lists and dictionaries. We talked about how you could address specific items in a list by giving the index. And the index is just the count of that letter from zero up to the end of the string. So, for example, H here's index zero.
He is index one Ella's index to the next Ellis Index three so on and so forth.
But you can use that to address the individual letters. So if, for example, we just wanted to print the first letter, we could do X
and you're gonna address it the exact same way you would with a list at zero is going to print the letter H.
But you can also print chunks of these letters or chunks of these strings at a time and you would do that in this case by doing X,
we'll say the index of the first letter we wanna print is zero on the index of the last letter we want to print
is for now. This isn't gonna work exactly like what you would think you would have initially assumed is gonna print h e l l
and then Oh, because 4024 is five letters
instead, however, is going to print h e l l. And the reason for that is because of the fact that just like with range indexes are not inclusive.
So if you wanted to print the 1st 5 letters, you would actually be doing index of zero
five and the colon. Here is how you identify all of the numbers between zero and five
you see here, that'll now print Hello.
But you can also start from different places. You can, for example, do X or the new Prince
In that case, you're only going to print one letter because remember, four is not included. So that is functionally the same thing is just doing except three. You could do 3 to 5 which will actually give you two letters.
there's something a little bit trickier. For example, you might do
5 to 3, in which case you're not gonna get anything because of the fact that you have to add a piece of information to this index to this slicks, and that is something we call a step. So the way you would actually make this work if you're trying to print backward from 5 to 3 is you're gonna add another colon, and that is going to tell it how to step through. In this case, we're going to say negative
And you see here it's gonna print comma, which, because that's the
fifth index or the fourth index and then the third index.
Now I'll demonstrate to you a little bit more easily without showing other values in it.
Uh, ex Colin Colin, Negative one.
This is a very useful shorthand that says, Print this string backwards
because we're starting at this at basically no given index ending without a specific given index, and then all you're going to do is step back or with negative one.
So this is a shortcut to, say, print the whole string backwards.
use different steps to increment by different amounts. So, for example, if you wanted to do,
all of the letters in the string but only every second letter
you would step by two.
And you see, you're only going to get half the letters.
You could do all of the letters
You could do all of the letters step by negative,
negative three. Excuse me?
Those are all the different kinds of ways that you can slice a string. And in our lab, you're going to put a whole bunch of time slicing it in every possible direction and learn about that a lot. But for now, we're gonna leave it there, and you're just gonna understand that that is what strings slicing and those worst string methods before that. So that's what we discussed in this video of the concept of methods, string methods and string slicing
in our supplemental material. In our exercises, we have ah,
bunch a bunch, a bunch of string slices for you to do so that you can get as familiar as possible with it and really just dig down into it. That's gonna be all for this video. I hope you enjoyed it. And I hope you found it informative. I was always in your instructor, Joe Perry, and thank you for watching this here on Cyber Eri on Demand and come back for our next video in which we're going to learn more about input