2.7 Strings (Deep-Dive) Part 2 - 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 »

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