Time
2 hours 57 minutes
Difficulty
Beginner
CEU/CPE
3

Video Transcription

00:00
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
00:17
document. Either way, excuse me. Either way,
00:19
the important thing is that you've gotten a little bit of experience programming
00:23
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,
00:39
numbers, lists and dictionaries, which were these next few labs or these next few lessons Rather,
00:44
it's gonna be a lot easier for you to understand if you've gotten comfortable right in your own python code.
00:50
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
01:10
functions that we just discussed.
01:12
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.
01:25
So we can use that to understand what methods are a little bit more effectively creating ourselves a variable X equals
01:32
hello
01:33
willing
01:37
Dr
01:38
X
01:38
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.
01:44
But
01:45
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
01:53
and upper
01:56
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.
02:08
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.
02:21
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.
02:38
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
02:58
my mind. I was amazed at the idea that you get so much done so easily with these men.
03:02
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
03:13
a list that you're trying to compare against some input. So, for example, we gonna have this list of Joe,
03:17
Jimmy,
03:20
Bob and Tim.
03:23
Look, I fix that,
03:25
and then we're going to have some input. What? We're just gonna call our input variable,
03:34
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.
03:46
What you can do is four I
03:50
in nameless.
03:53
Yeah,
03:53
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
04:14
If I equals input variable
04:18
dot lower
04:20
prince.
04:23
I found it.
04:25
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
04:30
because of the fact that input variable
04:33
lower
04:34
looks like that
04:35
now if we were just to do
04:40
for I am name list if I
04:43
equals equals input variable.
04:48
What
04:49
not what I meant to do.
05:00
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.
05:18
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,
05:25
And what we're talking about now is called strange slicing or string indexing. Now you may remember
05:30
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.
05:44
He is index one Ella's index to the next Ellis Index three so on and so forth.
05:49
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
05:57
zero,
05:59
and you're gonna address it the exact same way you would with a list at zero is going to print the letter H.
06:04
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,
06:12
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
06:16
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
06:24
and then Oh, because 4024 is five letters
06:28
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.
06:36
So if you wanted to print the 1st 5 letters, you would actually be doing index of zero
06:42
five and the colon. Here is how you identify all of the numbers between zero and five
06:46
you see here, that'll now print Hello.
06:49
But you can also start from different places. You can, for example, do X or the new Prince
06:57
X
06:58
from 34
07:02
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.
07:15
You're gonna also
07:15
there's something a little bit trickier. For example, you might do
07:18
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
07:39
one.
07:41
And you see here it's gonna print comma, which, because that's the
07:44
fifth index or the fourth index and then the third index.
07:47
Now I'll demonstrate to you a little bit more easily without showing other values in it.
07:53
Uh, ex Colin Colin, Negative one.
07:56
This is a very useful shorthand that says, Print this string backwards
08:01
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.
08:09
So this is a shortcut to, say, print the whole string backwards.
08:13
You can
08:15
use different steps to increment by different amounts. So, for example, if you wanted to do,
08:20
uh,
08:22
all of the letters in the string but only every second letter
08:26
you would step by two.
08:28
And you see, you're only going to get half the letters.
08:30
You could do all of the letters
08:31
step by three.
08:35
You could do all of the letters step by negative,
08:37
negative three. Excuse me?
08:39
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
08:58
in our supplemental material. In our exercises, we have ah,
09:01
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
09:20
and
09:20
output.

Up Next

Intro to Python

This is an introductory course on Python for cyber security, giving students the ability to understand the basics of the language, solve problems with scripts and identify useful Python modules.

Instructed By

Instructor Profile Image
Joe Perry
Senior Technical Instructor at FireEye, Inc
Instructor