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