2.6 Strings (Deep-Dive) Part 1 - IP
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Course
Difficulty
Beginner
Video Transcription
00:00
>> Hello, everyone. Welcome back to intro to
00:00
Python here on Cybrary OnDemand.
00:00
I, as always, I'm your instructor Joe Perry.
00:00
If you're watching this video,
00:00
it means that you have completed lab assignment.
00:00
Whether you did it through next tech or
00:00
spectacular partner organization or you did
00:00
it through just the assigned document.
00:00
Either way, the important thing
00:00
is that you've gotten a little bit of
00:00
experience programming,
00:00
gotten your hands-on Python,
00:00
and really gotten to actually write some code.
00:00
If you haven't done the lab yet,
00:00
I very highly recommend you go back and do that.
00:00
I, as a programmer, I never really got into programming.
00:00
I never really understood it until
00:00
I actually started to write the code myself.
00:00
As we go through strings,
00:00
numbers, lists, and dictionaries,
00:00
which are these next few lessons rather,
00:00
it's going to be a lot easier for you to understand
00:00
if you've gotten comfortable
00:00
writing your own Python code.
00:00
No further ado, less than six objectives,
00:00
we're going to learn about string methods.
00:00
We're going to talk about what methods are briefly,
00:00
we're going to understand, we're going to use
00:00
string slicing,
00:00
and we're going to learn to take input from the user,
00:00
and then we're going to learn to
00:00
write better print statements.
00:00
This lesson is going to be divided into two videos.
00:00
The first is going to be about string
00:00
methods and string slicing.
00:00
The second one is going to be about
00:00
the input-output functions that we just discussed.
00:00
String methods. What are method?
00:00
Well, if you remember from our very earliest video,
00:00
we talked about this dir function
00:00
and what that did is it showed us all of
00:00
the objects are all the functions and all of
00:00
the names that are defined in our current space.
00:00
We can use that to understand
00:00
what methods are a little bit more
00:00
effectively by creating ourselves a variable,
00:00
[NOISE] x equals hello,
00:00
world, dir x.
00:00
You see we have all these underscored names.
00:00
We're not going to worry about those right now,
00:00
those are internal and not really for our use.
00:00
But we look down here,
00:00
we see these names in the list
00:00
that don't have underscores.
00:00
For example, we see the ones that I'm going to focus on
00:00
right now, lower and upper.
00:00
What these are, are methods.
00:00
Methods are functions that are
00:00
attached to a given object.
00:00
Then our intermediate and advanced Python classes,
00:00
we spend a whole bunch of time
00:00
on classes and objects in Python.
00:00
We talked about object orientation.
00:00
In that time, you're going to learn under the hood
00:00
a lot more about what methods and attributes are,
00:00
and what they do,
00:00
and what they're for, and all that thing.
00:00
For now, just understand that methods are
00:00
functions which are attached to an object.
00:00
Let's clear our screen here a little bit.
00:00
Remember, we have x, which is our hello, world,
00:00
that first-ever script we ever wrote,
00:00
hello, world, and then we're going to do x.upper.
00:00
This is the way you use methods
00:00
that are attached to a given object.
00:00
If we're trying to use the upper method from x,
00:00
we do x.upper and then we call that function.
00:00
There is no arguments to the upper function,
00:00
we just call it and you
00:00
see that it's going to print HELLO,
00:00
WORLD, in all caps.
00:00
Now for those of you who've never written
00:00
assembly or C code,
00:00
it's hard to describe to you
00:00
how much more work it would
00:00
take to do this in those languages.
00:00
I addressed that because upper and
00:00
lower were two of the first functions I ever
00:00
learned in Python and it absolutely blew my mind.
00:00
I was amazed at the idea that you would get so
00:00
much done so easily with these methods.
00:00
That's how you use string methods,
00:00
and we're going to talk about a few more of them.
00:00
One of the ways that you can use lower,
00:00
which is the companion function
00:00
to upper really effectively,
00:00
is if you have [NOISE] a list that
00:00
you're trying to compare against some inputs.
00:00
For example, we're going to have this list of Joe,
00:00
Jimmy, Bob, and Tim, [NOISE] fix that.
00:00
Then we're going to have some input,
00:00
which we're just going to call our input variable.
00:00
[NOISE] You can see there that,
00:00
and normally you would get this from your user,
00:00
but we haven't discussed that just yet,
00:00
so we're not going to worry about it,
00:00
the input variable doesn't exactly
00:00
match the first name in this list.
00:00
It is the same name,
00:00
but the input has a capital letter in it.
00:00
What you can do is for i in name list if i.
00:00
[NOISE] Remember double equal in is are similar.
00:00
This is one of the cases in which you're going to find
00:00
a difference if you're performing
00:00
methods against your string,
00:00
you're going to want to use double equal instead of is.
00:00
Again, the concepts of identity are really
00:00
part of object orientation,
00:00
so we will discuss those in later classes.
00:00
For now, just understand
00:00
that if we're making use of a method,
00:00
it is safer to use the double equal.
00:00
If i equals input [NOISE]
00:00
variable.lower, print found it.
00:00
Now hopefully this is going to work for us.
00:00
Sure enough, it found name we were looking for because of
00:00
the fact that input
00:00
[NOISE] variable.lower looks like that.
00:00
Now, if we were just to do for i name list,
00:00
if i equals equals input variable,
00:00
[NOISE] not what I meant to do.
00:00
[NOISE] You can see
00:00
it will never print found it because of
00:00
the fact that those two names don't exactly match.
00:00
That's the concept of string methods.
00:00
There are a whole bunch of them here.
00:00
As I said, you can find them by doing dir x,
00:00
I highly recommend you play around with them,
00:00
things like isdecimal or isdigit.
00:00
You can find a lot of useful information
00:00
just by messing with those methods.
00:00
Now, however, we're going to
00:00
move on to the next concept.
00:00
For that, we're going to have our
00:00
first string here, hello, world.
00:00
What we're talking about now is called
00:00
string slicing or string indexing.
00:00
Now you may remember from very
00:00
briefly when we were talking
00:00
about lists and dictionaries,
00:00
we talked about how you could address
00:00
specific items in a list by giving the index.
00:00
The index is just the count of that letter,
00:00
from zero up to the end of the string.
00:00
For example, H here is index 0,
00:00
e is index 1,
00:00
l is index 2,
00:00
the next l is index 3,
00:00
so on, and so forth.
00:00
But you can use that to address the individual letters.
00:00
If for example, we just wanted to print the first letter,
00:00
we can do x 0
00:00
and you're going to address it the exact
00:00
same way you would with a list.
00:00
X 0 is going to print the letter
00:00
H. But you can also print chunks of these letters,
00:00
or chunks of these strings at a time.
00:00
You would do that in this case by doing x,
00:00
we'll say the index of the first letter
00:00
we want to print is zero and
00:00
the index of the last letter we want to print is four.
00:00
Now, this isn't going to work
00:00
exactly like what you would think.
00:00
You would initially assume this is going to print
00:00
H-E-L-L and then O,
00:00
because 0-4 is five letters.
00:00
Instead, however, it's going to print H-E-L-L.
00:00
The reason for that
00:00
is because of the fact that just like with range,
00:00
indexes are not inclusive.
00:00
If you wanted to print the first five letters,
00:00
you would actually be doing index of 0-5.
00:00
The colon here is how you identify
00:00
all of the numbers between zero and five.
00:00
You see here that it will now print hello.
00:00
But you can also start from different places.
00:00
You can, for example, [NOISE] do print x from 3-4.
00:00
In that case, you're only going to print
00:00
one letter because remember four is not included,
00:00
so that is functionally the same thing as
00:00
just doing x of 3.
00:00
You could do 3-5,
00:00
which will actually give you two letters.
00:00
You can also do something a little bit trickier.
00:00
For example, you might do 5-3,
00:00
in which case you're not going to get anything
00:00
because of the fact that you have
00:00
to add a piece of information to this index to the Slax.
00:00
That is something we call a step.
00:00
The way you would actually want to
00:00
make this work if you're trying
00:00
to print backward from 5-3,
00:00
is you're going to add another colon,
00:00
and that is going to tell it how to step through.
00:00
In this case, we're going to say negative 1.
00:00
You see here it's going to print,o which because that's
00:00
the fourth index and then the third index.
00:00
Now, I'll demonstrate to you a little bit more
00:00
easily without showing other values in it.
00:00
X:: negative 1.
00:00
This is a very useful shorthand that says print
00:00
this string backward because
00:00
word's starting at basically no given index
00:00
ending without a specific given index,
00:00
and then all you're going to do is
00:00
step backward with negative one.
00:00
This is a shortcut to say
00:00
print the whole string backward.
00:00
You can use different steps
00:00
to increment by different amounts.
00:00
For example, if you wanted to
00:00
do all of the letters in the string,
00:00
but only every second letter you would step
00:00
by two and you see
00:00
you're only going to get half the letters.
00:00
You can do all of the letters step
00:00
by negative 3, excuse me.
00:00
Those are all the different ways
00:00
that you can slice a string.
00:00
In our lab,
00:00
you're going to spend a whole bunch of time slicing it in
00:00
every possible direction and learn about that a lot.
00:00
But for now, we're going to leave it
00:00
there and you're just going to understand that
00:00
that is what string slicing and
00:00
those were string methods before that.
00:00
That's all we discussed in this video
00:00
or the concept of methods,
00:00
string methods, and string slicing.
00:00
In our supplemental material in our exercises,
00:00
we have a bunch of string slices for you to do so
00:00
that you can get as familiar as possible with
00:00
it and really just dig down into it.
00:00
That's going to be all for this video.
00:00
I hope you enjoyed it and I hope
00:00
you found it informative.
00:00
I, as always,
00:00
I'm your instructor, Joe Perry,
00:00
and thank you for watching this here on Cybrary OnDemand.
00:00
Come back for our next video
00:00
in which we are going to learn
00:00
more about input and output.
Up Next
2.7 Strings (Deep-Dive) Part 2 - IP
2.8 Numbers (Deep-Dive) - IP
2.9 Lists (Deep-Dive) - IP
2.10 Dictionaries (Deep-Dive) - IP
2.11 Summary and Review - IP
Similar Content