Time
2 hours 57 minutes
Difficulty
Beginner
CEU/CPE
3

Video Transcription

00:00
Hello, everyone. And welcome back to intrude a python here on cyber eri 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 six. In this video, we're gonna learn more about how to take in but from our users and how to write better print statements.
00:18
So to do that, we're gonna go back into our trusty terminal here. We've got Python three already up and running from the last video in the last lesson.
00:26
And in this video, we're gonna talk about, as I said, input and output in python three. The way we take input
00:32
is with
00:33
the input function.
00:35
You can see here that when I typed it not only didn't print anything the screen, it also didn't take me to a new line of the interpreter. That's because it is waiting on the user to provide some kind of input. So here, I'm going to do
00:45
hello World String that we like to use so much
00:48
and you see, it's going to go back to giving me a problem.
00:50
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.
00:58
But in fact those quotes aren't part of the string or are
01:02
the normal string definition line
01:04
as they would be in normal python because of the fact that Python assumed whatever we gave it was a strength.
01:10
And to demonstrate that a little bit better without my verbosity and rambling, you can see X equals input.
01:15
Hello, world.
01:15
So from this would be from the command line. And I'll show you how to do this from a script in just a second. So I'm typing this stream without quotes around it, and Python still takes it and knows that in reality, that's a strength.
01:26
And I'll show you. Like I said, I'm gonna show you that from
01:30
script here.
01:30
So we're gonna do them
01:34
with them. I owe, not key. Why
01:37
Door shebang Juan
01:46
and we're gonna create a function, take
01:49
input
01:52
and that's not gonna take any arguments at all.
01:53
And all that's going to do is they X equals input
01:57
return
02:00
that we will call that function down here below
02:15
and you see that nothing else is happening because we only took the input and returned it. We didn't actually print it anyway.
02:20
But
02:21
the other problem here is that we're not really very 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 gonna understand. Oh, this program is looking for input from me.
02:32
So the way we can do that
02:35
well, you can kind of fix this up a little bit, Make it more functional
02:38
is by providing an argument to input. And what we would do there
02:40
is We would write some prompt string. So, for example, input text,
02:47
and then
02:50
I'm here at the bottom will go ahead and just prince
02:53
the return from take input.
03:02
Now you see, we've got a prompted. It says input text.
03:07
Here we go
03:07
and it prints it back out for us.
03:09
So that's the way we actually take input in Python. It's with a very, very simple function called input.
03:15
What if we wanted to put things a little bit Maur for most of what I wanted to add a little bit more information, For example,
03:22
What if we wanted to print a string that had a lot of inputs. So perhaps we might want to say something like
03:28
and we'll do a comment here to describe the idea
03:30
print
03:32
you entered.
03:38
How would we go about doing that?
03:42
Well, the easiest way and the way that we're gonna do it here
03:45
Not necessarily the easiest way, but the best way and the way we're gonna be doing it here
03:47
is by using the method.
03:50
Format
03:51
and method is a string format is a string method
03:53
that is designed to input data into a string sort of in the abstract. So we could do, for example, and I'll actually show you this first from our interpreter.
04:06
Wrong key word. There we go.
04:09
S one equals.
04:11
You said
04:13
they were going to use these curly braces again, or brackets.
04:17
So that string right now just looks like this
04:20
format. The method format is a really, really cool method that's gonna take that those braces and say, OK, data needs to go in here. So
04:29
that's one dot
04:30
dot format,
04:35
and then we're going to do
04:38
input
04:42
and the end. But we're gonna give it his test,
04:44
and you see, it's going to say you said tests
04:46
well, the input method does. It goes back
04:49
to the string that it's addressed against, finds braces and inserts data into them just one piece of the time.
04:56
So if you want to say
04:58
you said
05:00
and
05:08
we would just give it the argument multiple times
05:15
you said test and test again. So you see here format is a very, 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, you, without any further input from you,
05:33
we're gonna go back into our iota pi
05:36
where we're going to say,
05:43
Take
05:44
well, we're gonna return,
05:46
Take input
05:51
into this stir variable stir var,
05:56
and then we're going to do
05:57
prints.
05:59
Stir bar
06:00
format.
06:05
Sorry, not stir Bardot format messing myself up here.
06:11
We're going to have a seven second variable,
06:15
which we will call prompted There. You know, I'm not getting back into order.
06:20
You
06:21
entered
06:25
and their braces
06:26
disturb our take input.
06:28
Now we're going to print
06:30
prompted a radio. Now we're cooking with grease
06:33
format
06:34
Mr Vaughn
06:39
color at number of parentheses. Everything matches,
06:42
and we'll run our code and see if it works.
06:47
There you go. So that's how you can take input and give back out. Put in more formatted, more useful ways. You can use that to put numbers list 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. So please take your time, dig in and play with that just as much as you need to to get familiar with it.
07:05
Thank you for watching this video again. We discussed the ideas of taking input and writing better print statements.
07:11
I am old. I, as always, rather am your instructor, Joe Perry. And I'm thrilled that you came to watch this video and I can't wait for you to come back for a lesson seven. Where we're gonna talk about numbers. Thanks for watching Internet Python here on Cyber Eri on demand

Up Next

Intro to Python

This is an introductory course on Python for cyber security, giving students the ability to understand the basics of the language, solve problems with scripts and identify useful Python modules.

Instructed By

Instructor Profile Image
Joe Perry
Senior Technical Instructor at FireEye, Inc
Instructor