WEBVTT

00:00.000 --> 00:03.030
>> Hello everyone and welcome
back to Intro to Python

00:03.030 --> 00:05.610
here on Cybrary On Demand.

00:05.610 --> 00:07.650
I as always, I'm your
instructor Joe Perry,

00:07.650 --> 00:09.570
and I'm just thrilled to
have you back here for

00:09.570 --> 00:12.000
the second video of Lesson 6.

00:12.000 --> 00:13.110
In this video, we're
going to learn

00:13.110 --> 00:14.280
more about how to
take input from

00:14.280 --> 00:17.815
our users and how to write
better print statements.

00:17.815 --> 00:19.310
To do that, we're going to

00:19.310 --> 00:20.775
go back into our
Trusty terminal here,

00:20.775 --> 00:22.520
we've got Python
3 already up and

00:22.520 --> 00:25.530
running from the last
video in the last lesson.

00:25.540 --> 00:28.010
In this video, we're
going to talk about,

00:28.010 --> 00:29.380
as I said, input and output.

00:29.380 --> 00:30.920
In Python 3, the way we take

00:30.920 --> 00:35.030
input is with the
input function.

00:35.030 --> 00:36.485
Now you can see here
that when I typed it,

00:36.485 --> 00:38.030
it not only didn't print
anything to the screen,

00:38.030 --> 00:38.900
it also didn't take me to

00:38.900 --> 00:40.145
a new line with the interpreter.

00:40.145 --> 00:41.570
That's because it is waiting on

00:41.570 --> 00:43.670
the user to provide some input.

00:43.670 --> 00:45.860
Here I'm going to do, that

00:45.860 --> 00:48.295
Hello World string that
we like to use so much,

00:48.295 --> 00:50.750
and you see it's going to go
back to giving me a prompt.

00:50.750 --> 00:52.895
Now we can see that the
value that we gave it,

00:52.895 --> 00:55.235
the Hello World, is stored in x.

00:55.235 --> 00:56.525
Now when I wrote that,

00:56.525 --> 00:59.075
I put quotes around
it, but in fact,

00:59.075 --> 01:01.325
those quotes aren't
part of the string

01:01.325 --> 01:04.430
or aren't the normal
string definition

01:04.430 --> 01:06.890
line as they would be in
normal Python because of

01:06.890 --> 01:08.000
the fact that Python

01:08.000 --> 01:10.160
assumed whatever we
gave it was a string.

01:10.160 --> 01:11.720
To demonstrate that a little bit

01:11.720 --> 01:13.330
better without my
verbosity and rambling,

01:13.330 --> 01:16.980
you can see x equals
input, Hello World.

01:16.980 --> 01:19.250
This would be from
the command line and

01:19.250 --> 01:20.030
I'll show you how
to do this from

01:20.030 --> 01:21.320
a script in just a second.

01:21.320 --> 01:23.540
I'm typing the string
without quotes around it.

01:23.540 --> 01:25.010
Python's still
takes it and knows

01:25.010 --> 01:27.145
that in reality,
that's a string.

01:27.145 --> 01:28.910
Like I said, I'm going
to show you that

01:28.910 --> 01:31.790
from a script here.

01:31.790 --> 01:37.235
We're going to do vim io.py,

01:37.235 --> 01:39.900
do our usr/bin line,

01:45.080 --> 01:51.580
and we're going to create
a function, take_input.

01:51.580 --> 01:54.110
That's not going to take
any arguments at all.

01:54.110 --> 02:00.640
All that's going to do is say
x equals input, return x.

02:00.640 --> 02:03.650
Then we will call that
function down here below.

02:03.650 --> 02:15.470
[NOISE]

02:15.470 --> 02:17.000
You see that nothing
else is happening

02:17.000 --> 02:18.740
because we only took the
input and returned it,

02:18.740 --> 02:20.435
we didn't actually
print it anywhere.

02:20.435 --> 02:25.010
But the other problem here is
that it's not very verbose.

02:25.010 --> 02:26.690
It's not telling us
it wants anything.

02:26.690 --> 02:28.280
We're not understanding
necessarily.

02:28.280 --> 02:29.420
If we didn't write
the code, we're not

02:29.420 --> 02:30.755
necessarily going to understand,

02:30.755 --> 02:33.140
oh, this program is
looking for input for me.

02:33.140 --> 02:34.655
The way we can do that,

02:34.655 --> 02:36.255
the way we can

02:36.255 --> 02:38.255
fix this up a little bit
make it more functional,

02:38.255 --> 02:40.280
is by providing an
argument to input.

02:40.280 --> 02:41.660
What we would do there

02:41.660 --> 02:43.535
is we would write
some prompt string.

02:43.535 --> 02:46.620
For example, input text,

02:47.830 --> 02:51.050
and then down here
at the bottom,

02:51.050 --> 02:52.770
we'll go ahead and just print

02:52.770 --> 02:54.650
the return from the take_input.

02:54.650 --> 03:03.700
[NOISE] Now you see

03:03.700 --> 03:06.415
we've got a prompt and
it says input text.

03:06.415 --> 03:10.645
Here we go and it prints
it back out for us.

03:10.645 --> 03:12.970
That's the way we actually
take input in Python.

03:12.970 --> 03:16.060
It's with a very, very simple
function called input.

03:16.060 --> 03:17.680
But what if we wanted
to print things a

03:17.680 --> 03:19.210
little bit more verbosely?

03:19.210 --> 03:21.270
What if we wanted to add a
little bit more information?

03:21.270 --> 03:23.530
For example, what
if we wanted to

03:23.530 --> 03:26.065
print a string that
had a lot of inputs?

03:26.065 --> 03:27.820
Perhaps we might want
to say something

03:27.820 --> 03:31.510
like and we'll do a comment
here to describe the idea.

03:31.510 --> 03:35.450
Print you entered.

03:38.920 --> 03:41.870
How would we go
about doing that?

03:41.870 --> 03:43.520
Well, the easiest way

03:43.520 --> 03:44.930
and the way that we're
going to do it here,

03:44.930 --> 03:46.280
not necessarily the easiest way,

03:46.280 --> 03:48.020
but the best way and the way
we're gong to be doing it

03:48.020 --> 03:51.960
here is by using
the method format.

03:52.000 --> 03:56.030
Format is a string method
that is designed to

03:56.030 --> 03:59.675
input data into a
string in the abstract.

03:59.675 --> 04:00.950
We could do, for example,

04:00.950 --> 04:01.850
and I'll actually show you this

04:01.850 --> 04:03.900
first from our interpreter.

04:06.050 --> 04:09.300
Wrong keyword. There we go.

04:09.300 --> 04:13.365
S1 equals you said,

04:13.365 --> 04:15.605
and then we're going to
use these curly braces

04:15.605 --> 04:17.910
or brackets.

04:18.020 --> 04:21.450
That string right now
just looks like this.

04:21.450 --> 04:23.930
The method format is a really,

04:23.930 --> 04:25.070
really cool method
that's going to

04:25.070 --> 04:26.870
take those braces and say,

04:26.870 --> 04:28.475
data needs to go in here.

04:28.475 --> 04:36.750
So s1.format,

04:36.750 --> 04:39.790
and then we're
going to do input.

04:42.110 --> 04:44.745
The input, we're going
to give it as test,

04:44.745 --> 04:47.280
and you see it's going
to say you said test.

04:47.280 --> 04:49.340
What the input method
does is it goes

04:49.340 --> 04:52.010
back to the string that
it's addressed against,

04:52.010 --> 04:53.750
finds braces, and inserts

04:53.750 --> 04:55.925
data into them just
one piece at a time.

04:55.925 --> 04:59.940
If we wanted to say, you said,

05:00.040 --> 05:09.080
and, we would

05:09.080 --> 05:12.120
just give it the
argument multiple times.

05:15.890 --> 05:18.840
You said test and test again.

05:18.840 --> 05:21.530
You see here format is a
very easy to use function.

05:21.530 --> 05:22.780
You just give it the arguments

05:22.780 --> 05:24.440
and so long as the number
of arguments you give

05:24.440 --> 05:27.595
it match the number of braces
or the sets of braces,

05:27.595 --> 05:29.060
it's going to be able
to input that without

05:29.060 --> 05:32.820
any further input from you.

05:33.460 --> 05:36.935
We're going to go
back into our io.py,

05:36.935 --> 05:39.900
where we're going to say,

05:44.150 --> 05:54.435
we're going to return take
input into this str variable,

05:54.435 --> 05:56.730
str_var, and then we're going

05:56.730 --> 06:00.880
to do print str_var.format.

06:05.900 --> 06:08.265
Sorry not str_var.format,

06:08.265 --> 06:10.330
messing myself up here.

06:10.390 --> 06:14.640
We're going to have
a second variable,

06:15.980 --> 06:18.405
which we will call prompted.

06:18.405 --> 06:20.955
There we go. Now I'm
getting it back into order.

06:20.955 --> 06:26.625
You entered, and
then our braces,

06:26.625 --> 06:28.445
str_var, take input.

06:28.445 --> 06:31.805
Now we're going to
print prompted.

06:31.805 --> 06:34.040
There we go. Now we're
cooking with grease.

06:34.040 --> 06:37.660
Format, str_var.

06:38.380 --> 06:41.150
Got the right number
of parentheses,

06:41.150 --> 06:43.040
everything matches, and

06:43.040 --> 06:45.990
we'll run our code
and see if it works.

06:47.870 --> 06:51.020
There you go. That's how
you can take input and

06:51.020 --> 06:53.660
give back output in more
formatted, more useful ways.

06:53.660 --> 06:54.950
You can use that to put numbers,

06:54.950 --> 06:56.210
lists, strings, dictionaries,

06:56.210 --> 06:57.890
whatever you want
into your strings

06:57.890 --> 06:59.570
just by using the format method.

06:59.570 --> 07:00.620
You will have more examples

07:00.620 --> 07:01.850
of that in the lab
that's coming up.

07:01.850 --> 07:03.650
Please take your time, dig in,

07:03.650 --> 07:04.880
and play with that just as much

07:04.880 --> 07:06.440
as you need to get
familiar with it.

07:06.440 --> 07:08.045
Thank you for
watching this video.

07:08.045 --> 07:09.470
Again, we discussed the ideas of

07:09.470 --> 07:11.815
taking input and writing
better print statements.

07:11.815 --> 07:14.160
I as always, rather,

07:14.160 --> 07:15.650
am your instructor, Joe Perry.

07:15.650 --> 07:17.760
I'm thrilled that you
came to watch this video.

07:17.760 --> 07:20.060
I can't wait for you to
come back for Lesson 7,

07:20.060 --> 07:21.740
where we're going to
talk about numbers.

07:21.740 --> 07:23.420
Thanks for watching
intro to Python

07:23.420 --> 07:26.400
here on Cybrary On Demand.

