WEBVTT

00:00.000 --> 00:02.040
>> Hello, everyone.
Welcome back to intro to

00:02.040 --> 00:04.215
Python here on Cybrary OnDemand.

00:04.215 --> 00:06.930
I, as always, I'm your
instructor Joe Perry.

00:06.930 --> 00:08.220
If you're watching this video,

00:08.220 --> 00:11.520
it means that you have
completed lab assignment.

00:11.520 --> 00:13.020
Whether you did it
through next tech or

00:13.020 --> 00:14.880
spectacular partner
organization or you did

00:14.880 --> 00:18.135
it through just the
assigned document.

00:18.135 --> 00:20.520
Either way, the important thing

00:20.520 --> 00:21.570
is that you've gotten
a little bit of

00:21.570 --> 00:23.205
experience programming,

00:23.205 --> 00:24.840
gotten your hands-on Python,

00:24.840 --> 00:26.550
and really gotten to
actually write some code.

00:26.550 --> 00:28.350
If you haven't done the lab yet,

00:28.350 --> 00:31.155
I very highly recommend
you go back and do that.

00:31.155 --> 00:34.070
I, as a programmer, I never
really got into programming.

00:34.070 --> 00:35.900
I never really
understood it until

00:35.900 --> 00:37.915
I actually started to
write the code myself.

00:37.915 --> 00:39.830
As we go through strings,

00:39.830 --> 00:41.720
numbers, lists,
and dictionaries,

00:41.720 --> 00:44.645
which are these next
few lessons rather,

00:44.645 --> 00:47.045
it's going to be a lot
easier for you to understand

00:47.045 --> 00:48.380
if you've gotten comfortable

00:48.380 --> 00:49.925
writing your own Python code.

00:49.925 --> 00:52.270
No further ado, less
than six objectives,

00:52.270 --> 00:53.430
we're going to learn
about string methods.

00:53.430 --> 00:55.625
We're going to talk about
what methods are briefly,

00:55.625 --> 00:56.870
we're going to understand,
we're going to use

00:56.870 --> 00:58.010
string slicing,

00:58.010 --> 01:00.530
and we're going to learn to
take input from the user,

01:00.530 --> 01:01.790
and then we're going to learn to

01:01.790 --> 01:02.990
write better print statements.

01:02.990 --> 01:05.180
This lesson is going to be
divided into two videos.

01:05.180 --> 01:06.440
The first is going
to be about string

01:06.440 --> 01:07.850
methods and string slicing.

01:07.850 --> 01:09.200
The second one is
going to be about

01:09.200 --> 01:12.505
the input-output functions
that we just discussed.

01:12.505 --> 01:15.360
String methods. What are method?

01:15.360 --> 01:17.930
Well, if you remember from
our very earliest video,

01:17.930 --> 01:19.700
we talked about
this dir function

01:19.700 --> 01:21.140
and what that did is
it showed us all of

01:21.140 --> 01:22.760
the objects are all the
functions and all of

01:22.760 --> 01:25.675
the names that are defined
in our current space.

01:25.675 --> 01:27.110
We can use that to understand

01:27.110 --> 01:28.130
what methods are
a little bit more

01:28.130 --> 01:30.380
effectively by creating
ourselves a variable,

01:30.380 --> 01:33.140
[NOISE] x equals hello,

01:33.140 --> 01:39.315
world, dir x.

01:39.315 --> 01:41.690
You see we have all
these underscored names.

01:41.690 --> 01:42.740
We're not going to worry
about those right now,

01:42.740 --> 01:44.815
those are internal and
not really for our use.

01:44.815 --> 01:46.400
But we look down here,

01:46.400 --> 01:47.855
we see these names in the list

01:47.855 --> 01:49.030
that don't have underscores.

01:49.030 --> 01:51.860
For example, we see the ones
that I'm going to focus on

01:51.860 --> 01:55.510
right now, lower and upper.

01:55.510 --> 01:57.640
What these are, are methods.

01:57.640 --> 01:59.320
Methods are functions that are

01:59.320 --> 02:01.480
attached to a given object.

02:01.480 --> 02:03.610
Then our intermediate and
advanced Python classes,

02:03.610 --> 02:05.560
we spend a whole bunch of time

02:05.560 --> 02:07.735
on classes and
objects in Python.

02:07.735 --> 02:09.680
We talked about
object orientation.

02:09.680 --> 02:11.530
In that time, you're going
to learn under the hood

02:11.530 --> 02:13.570
a lot more about what
methods and attributes are,

02:13.570 --> 02:14.050
and what they do,

02:14.050 --> 02:15.595
and what they're for,
and all that thing.

02:15.595 --> 02:17.530
For now, just understand
that methods are

02:17.530 --> 02:21.020
functions which are
attached to an object.

02:21.020 --> 02:22.820
Let's clear our screen
here a little bit.

02:22.820 --> 02:24.730
Remember, we have x, which
is our hello, world,

02:24.730 --> 02:26.745
that first-ever
script we ever wrote,

02:26.745 --> 02:29.535
hello, world, and then
we're going to do x.upper.

02:29.535 --> 02:31.420
This is the way you use methods

02:31.420 --> 02:32.930
that are attached
to a given object.

02:32.930 --> 02:35.350
If we're trying to use
the upper method from x,

02:35.350 --> 02:38.005
we do x.upper and then
we call that function.

02:38.005 --> 02:40.085
There is no arguments
to the upper function,

02:40.085 --> 02:41.060
we just call it and you

02:41.060 --> 02:42.170
see that it's going
to print HELLO,

02:42.170 --> 02:43.580
WORLD, in all caps.

02:43.580 --> 02:44.990
Now for those of you
who've never written

02:44.990 --> 02:46.640
assembly or C code,

02:46.640 --> 02:48.260
it's hard to describe to you

02:48.260 --> 02:49.640
how much more work it would

02:49.640 --> 02:51.455
take to do this in
those languages.

02:51.455 --> 02:53.390
I addressed that
because upper and

02:53.390 --> 02:55.190
lower were two of the
first functions I ever

02:55.190 --> 02:58.820
learned in Python and it
absolutely blew my mind.

02:58.820 --> 03:00.860
I was amazed at the idea
that you would get so

03:00.860 --> 03:03.440
much done so easily
with these methods.

03:03.440 --> 03:05.390
That's how you use
string methods,

03:05.390 --> 03:06.940
and we're going to talk
about a few more of them.

03:06.940 --> 03:09.080
One of the ways that
you can use lower,

03:09.080 --> 03:10.100
which is the companion function

03:10.100 --> 03:11.525
to upper really effectively,

03:11.525 --> 03:13.700
is if you have
[NOISE] a list that

03:13.700 --> 03:15.740
you're trying to compare
against some inputs.

03:15.740 --> 03:17.900
For example, we're going
to have this list of Joe,

03:17.900 --> 03:25.690
Jimmy, Bob, and Tim,
[NOISE] fix that.

03:25.690 --> 03:27.695
Then we're going to
have some input,

03:27.695 --> 03:29.960
which we're just going to
call our input variable.

03:29.960 --> 03:35.775
[NOISE] You can see there that,

03:35.775 --> 03:37.880
and normally you would
get this from your user,

03:37.880 --> 03:39.020
but we haven't discussed
that just yet,

03:39.020 --> 03:40.010
so we're not going
to worry about it,

03:40.010 --> 03:41.720
the input variable
doesn't exactly

03:41.720 --> 03:43.865
match the first
name in this list.

03:43.865 --> 03:45.290
It is the same name,

03:45.290 --> 03:47.210
but the input has a
capital letter in it.

03:47.210 --> 03:55.350
What you can do is for
i in name list if i.

03:55.350 --> 03:57.720
[NOISE] Remember double
equal in is are similar.

03:57.720 --> 03:59.720
This is one of the cases in
which you're going to find

03:59.720 --> 04:00.950
a difference if
you're performing

04:00.950 --> 04:02.555
methods against your string,

04:02.555 --> 04:04.835
you're going to want to use
double equal instead of is.

04:04.835 --> 04:06.800
Again, the concepts of
identity are really

04:06.800 --> 04:08.940
part of object orientation,

04:08.940 --> 04:10.740
so we will discuss
those in later classes.

04:10.740 --> 04:11.810
For now, just understand

04:11.810 --> 04:13.025
that if we're making
use of a method,

04:13.025 --> 04:14.765
it is safer to use
the double equal.

04:14.765 --> 04:18.950
If i equals input [NOISE]

04:18.950 --> 04:24.875
variable.lower, print found it.

04:24.875 --> 04:26.960
Now hopefully this is
going to work for us.

04:26.960 --> 04:31.350
Sure enough, it found name we
were looking for because of

04:31.350 --> 04:32.550
the fact that input

04:32.550 --> 04:36.585
[NOISE] variable.lower
looks like that.

04:36.585 --> 04:41.355
Now, if we were just
to do for i name list,

04:41.355 --> 04:44.730
if i equals equals
input variable,

04:44.730 --> 04:51.270
[NOISE] not what I meant to do.

04:51.270 --> 05:00.820
[NOISE] You can see

05:00.820 --> 05:02.470
it will never print
found it because of

05:02.470 --> 05:04.900
the fact that those two
names don't exactly match.

05:04.900 --> 05:06.550
That's the concept
of string methods.

05:06.550 --> 05:08.080
There are a whole
bunch of them here.

05:08.080 --> 05:10.450
As I said, you can find
them by doing dir x,

05:10.450 --> 05:12.310
I highly recommend you
play around with them,

05:12.310 --> 05:14.290
things like isdecimal
or isdigit.

05:14.290 --> 05:16.495
You can find a lot of
useful information

05:16.495 --> 05:19.080
just by messing
with those methods.

05:19.080 --> 05:20.800
Now, however, we're going to

05:20.800 --> 05:22.305
move on to the next concept.

05:22.305 --> 05:23.835
For that, we're
going to have our

05:23.835 --> 05:25.890
first string here, hello, world.

05:25.890 --> 05:27.520
What we're talking
about now is called

05:27.520 --> 05:29.680
string slicing or
string indexing.

05:29.680 --> 05:31.720
Now you may remember from very

05:31.720 --> 05:32.530
briefly when we were talking

05:32.530 --> 05:34.000
about lists and dictionaries,

05:34.000 --> 05:35.080
we talked about how
you could address

05:35.080 --> 05:37.645
specific items in a list
by giving the index.

05:37.645 --> 05:39.835
The index is just the
count of that letter,

05:39.835 --> 05:41.980
from zero up to the
end of the string.

05:41.980 --> 05:44.660
For example, H here is index 0,

05:44.660 --> 05:46.140
e is index 1,

05:46.140 --> 05:47.415
l is index 2,

05:47.415 --> 05:48.660
the next l is index 3,

05:48.660 --> 05:50.200
so on, and so forth.

05:50.200 --> 05:53.310
But you can use that to address
the individual letters.

05:53.310 --> 05:56.120
If for example, we just wanted
to print the first letter,

05:56.120 --> 05:58.730
we can do x 0

05:58.730 --> 06:00.560
and you're going to
address it the exact

06:00.560 --> 06:02.165
same way you would with a list.

06:02.165 --> 06:04.265
X 0 is going to print the letter

06:04.265 --> 06:07.610
H. But you can also print
chunks of these letters,

06:07.610 --> 06:09.305
or chunks of these
strings at a time.

06:09.305 --> 06:12.340
You would do that in
this case by doing x,

06:12.340 --> 06:14.330
we'll say the index
of the first letter

06:14.330 --> 06:15.470
we want to print is zero and

06:15.470 --> 06:18.410
the index of the last letter
we want to print is four.

06:18.410 --> 06:19.460
Now, this isn't going to work

06:19.460 --> 06:20.610
exactly like what
you would think.

06:20.610 --> 06:22.850
You would initially assume
this is going to print

06:22.850 --> 06:25.695
H-E-L-L and then O,

06:25.695 --> 06:28.505
because 0-4 is five letters.

06:28.505 --> 06:31.190
Instead, however, it's
going to print H-E-L-L.

06:31.190 --> 06:32.960
The reason for that

06:32.960 --> 06:35.314
is because of the fact
that just like with range,

06:35.314 --> 06:37.540
indexes are not inclusive.

06:37.540 --> 06:39.320
If you wanted to print
the first five letters,

06:39.320 --> 06:42.350
you would actually be
doing index of 0-5.

06:42.350 --> 06:44.990
The colon here is
how you identify

06:44.990 --> 06:47.450
all of the numbers
between zero and five.

06:47.450 --> 06:50.090
You see here that it
will now print hello.

06:50.090 --> 06:53.000
But you can also start
from different places.

06:53.000 --> 07:03.400
You can, for example,
[NOISE] do print x from 3-4.

07:03.400 --> 07:04.850
In that case, you're
only going to print

07:04.850 --> 07:07.160
one letter because remember
four is not included,

07:07.160 --> 07:08.930
so that is functionally
the same thing as

07:08.930 --> 07:10.865
just doing x of 3.

07:10.865 --> 07:12.485
You could do 3-5,

07:12.485 --> 07:14.545
which will actually
give you two letters.

07:14.545 --> 07:17.600
You can also do something
a little bit trickier.

07:17.600 --> 07:20.330
For example, you might do 5-3,

07:20.330 --> 07:21.905
in which case you're not
going to get anything

07:21.905 --> 07:23.450
because of the
fact that you have

07:23.450 --> 07:26.915
to add a piece of information
to this index to the Slax.

07:26.915 --> 07:29.370
That is something
we call a step.

07:29.370 --> 07:30.290
The way you would
actually want to

07:30.290 --> 07:31.130
make this work if you're trying

07:31.130 --> 07:33.065
to print backward from 5-3,

07:33.065 --> 07:34.820
is you're going to
add another colon,

07:34.820 --> 07:37.685
and that is going to tell
it how to step through.

07:37.685 --> 07:40.675
In this case, we're
going to say negative 1.

07:40.675 --> 07:43.980
You see here it's going to
print,o which because that's

07:43.980 --> 07:47.665
the fourth index and
then the third index.

07:47.665 --> 07:50.075
Now, I'll demonstrate to
you a little bit more

07:50.075 --> 07:52.940
easily without showing
other values in it.

07:52.940 --> 07:56.120
X:: negative 1.

07:56.120 --> 07:58.790
This is a very useful
shorthand that says print

07:58.790 --> 08:01.460
this string backward because

08:01.460 --> 08:05.195
word's starting at
basically no given index

08:05.195 --> 08:07.280
ending without a
specific given index,

08:07.280 --> 08:08.630
and then all you're
going to do is

08:08.630 --> 08:10.100
step backward with negative one.

08:10.100 --> 08:11.510
This is a shortcut to say

08:11.510 --> 08:13.525
print the whole string backward.

08:13.525 --> 08:16.774
You can use different steps

08:16.774 --> 08:18.270
to increment by
different amounts.

08:18.270 --> 08:19.400
For example, if you wanted to

08:19.400 --> 08:24.025
do all of the letters
in the string,

08:24.025 --> 08:26.950
but only every second
letter you would step

08:26.950 --> 08:28.420
by two and you see

08:28.420 --> 08:30.340
you're only going to
get half the letters.

08:30.340 --> 08:33.160
You can do all of
the letters step

08:33.160 --> 08:39.820
by negative 3, excuse me.

08:39.820 --> 08:41.840
Those are all the different ways

08:41.840 --> 08:43.115
that you can slice a string.

08:43.115 --> 08:43.690
In our lab,

08:43.690 --> 08:45.430
you're going to spend a whole
bunch of time slicing it in

08:45.430 --> 08:48.355
every possible direction
and learn about that a lot.

08:48.355 --> 08:49.750
But for now, we're
going to leave it

08:49.750 --> 08:51.010
there and you're just
going to understand that

08:51.010 --> 08:52.810
that is what string slicing and

08:52.810 --> 08:54.870
those were string
methods before that.

08:54.870 --> 08:56.230
That's all we discussed
in this video

08:56.230 --> 08:57.280
or the concept of methods,

08:57.280 --> 08:59.065
string methods, and
string slicing.

08:59.065 --> 09:01.180
In our supplemental
material in our exercises,

09:01.180 --> 09:04.850
we have a bunch of string
slices for you to do so

09:04.850 --> 09:06.620
that you can get as
familiar as possible with

09:06.620 --> 09:08.610
it and really just
dig down into it.

09:08.610 --> 09:10.145
That's going to be
all for this video.

09:10.145 --> 09:11.330
I hope you enjoyed it and I hope

09:11.330 --> 09:12.470
you found it informative.

09:12.470 --> 09:13.760
I, as always,

09:13.760 --> 09:14.870
I'm your instructor, Joe Perry,

09:14.870 --> 09:18.200
and thank you for watching
this here on Cybrary OnDemand.

09:18.200 --> 09:19.280
Come back for our next video

09:19.280 --> 09:20.060
in which we are going to learn

09:20.060 --> 09:23.520
more about input and output.

