>> Hello everyone and welcome back to Intro to Python
here on Cybrary On Demand.
I as always, I'm your instructor Joe Perry,
and I'm just thrilled to have you back here for
the second video of Lesson 6.
In this video, we're going to learn
more about how to take input from
our users and how to write better print statements.
To do that, we're going to
go back into our Trusty terminal here,
we've got Python 3 already up and
running from the last video in the last lesson.
In this video, we're going to talk about,
as I said, input and output.
In Python 3, the way we take
input is with the input function.
Now you can see here that when I typed it,
it not only didn't print anything to the screen,
it also didn't take me to
a new line with the interpreter.
That's because it is waiting on
the user to provide some input.
Here I'm going to do, that
Hello World string that we like to use so much,
and you see it's going to go back to giving me a prompt.
Now we can see that the value that we gave it,
the Hello World, is stored in x.
Now when I wrote that,
I put quotes around it, but in fact,
those quotes aren't part of the string
or aren't the normal string definition
line as they would be in normal Python because of
the fact that Python
assumed whatever we gave it was a string.
To demonstrate that a little bit
better without my verbosity and rambling,
you can see x equals input, Hello World.
This would be from the command line and
I'll show you how to do this from
a script in just a second.
I'm typing the string without quotes around it.
Python's still takes it and knows
that in reality, that's a string.
Like I said, I'm going to show you that
from a script here.
We're going to do vim io.py,
do our usr/bin line,
and we're going to create a function, take_input.
That's not going to take any arguments at all.
All that's going to do is say x equals input, return x.
Then we will call that function down here below.
[NOISE]
You see that nothing else is happening
because we only took the input and returned it,
we didn't actually print it anywhere.
But the other problem here is that it's not very verbose.
It's not telling us it wants anything.
We're not understanding necessarily.
If we didn't write the code, we're not
necessarily going to understand,
oh, this program is looking for input for me.
The way we can do that,
the way we can
fix this up a little bit make it more functional,
is by providing an argument to input.
What we would do there
is we would write some prompt string.
For example, input text,
and then down here at the bottom,
we'll go ahead and just print
the return from the take_input.
[NOISE] Now you see
we've got a prompt and it says input text.
Here we go and it prints it back out for us.
That's the way we actually take input in Python.
It's with a very, very simple function called input.
But what if we wanted to print things a
little bit more verbosely?
What if we wanted to add a little bit more information?
For example, what if we wanted to
print a string that had a lot of inputs?
Perhaps we might want to say something
like and we'll do a comment here to describe the idea.
Print you entered.
How would we go about doing that?
Well, the easiest way
and the way that we're going to do it here,
not necessarily the easiest way,
but the best way and the way we're gong to be doing it
here is by using the method format.
Format is a string method that is designed to
input data into a string in the abstract.
We could do, for example,
and I'll actually show you this
first from our interpreter.
Wrong keyword. There we go.
S1 equals you said,
and then we're going to use these curly braces
or brackets.
That string right now just looks like this.
The method format is a really,
really cool method that's going to
take those braces and say,
data needs to go in here.
So s1.format,
and then we're going to do input.
The input, we're going to give it as test,
and you see it's going to say you said test.
What the input method does is it goes
back to the string that it's addressed against,
finds braces, and inserts
data into them just one piece at a time.
If we wanted to say, you said,
and, we would
just give it the argument multiple times.
You said test and test again.
You see here format is a very easy to use function.
You just give it the arguments
and so long as the number of arguments you give
it match the number of braces or the sets of braces,
it's going to be able to input that without
any further input from you.
We're going to go back into our io.py,
where we're going to say,
we're going to return take input into this str variable,
str_var, and then we're going
to do print str_var.format.
Sorry not str_var.format,
messing myself up here.
We're going to have a second variable,
which we will call prompted.
There we go. Now I'm getting it back into order.
You entered, and then our braces,
str_var, take input.
Now we're going to print prompted.
There we go. Now we're cooking with grease.
Format, str_var.
Got the right number of parentheses,
everything matches, and
we'll run our code and see if it works.
There you go. That's how you can take input and
give back output in more formatted, more useful ways.
You can use that to put numbers,
lists, strings, dictionaries,
whatever you want into your strings
just by using the format method.
You will have more examples
of that in the lab that's coming up.
Please take your time, dig in,
and play with that just as much
as you need to get familiar with it.
Thank you for watching this video.
Again, we discussed the ideas of
taking input and writing better print statements.
I as always, rather,
am your instructor, Joe Perry.
I'm thrilled that you came to watch this video.
I can't wait for you to come back for Lesson 7,
where we're going to talk about numbers.
Thanks for watching intro to Python
here on Cybrary On Demand.