Interacting With User Input
00:00 Now that you’ve seen how to work with string methods, it’s time for you to make things interactive.
00:07
In this lesson, you’ll learn how to get some input from a user with the built-in input()
function. You’ll write a program that asks a user to input some text and then display that text back to them, and you’ll also practice applying some of those methods that you learned in the last lesson. All right, the rest of this lesson is all going to be inside of IDLE.
00:33
Here in the interactive window, I’m going to have you practice with the built-in input()
function. Type input
, and you can see that it identifies it as a function.
00:44
If you open the parentheses to prepare to call it, you can see that it provides some additional information. The input()
function can accept a prompt that will prompt the user.
00:56
It will be normally text that’s in there. By default, it doesn’t have one. It will be set to None
. It also says that this function reads a string from standard input.
01:08 Let’s go ahead and close that other parenthesis to call it.
01:13
A newline appears, and it prompts you on the next line to type something—in this case, the user, or whoever’s running this. So I’ll see Hello There!
And when I press return, it returns a string literal of Hello There!
To make input()
be a little more user-friendly, you might have noticed that it asked that I could provide a prompt to display to the user.
01:39
The prompt is just a string that you put between the parentheses of input()
. It can be anything you want—a word, a symbol, or a phrase. So again, type in input()
,
01:51 and it might be helpful to have a space after it.
01:57 And again, it returns the string literal of what was typed in by the user. Let’s change this up and create a program, one that we can define the prompt, and then also maybe print out a a message using some of the skills you’ve learned up to now.
02:17 I’m starting with a brand-new interactive window. I need to have you open up IDLE’s editor window. You can go to File and New, or you can use a key command, which is what I’m going to use here, which is just Command + N, or on Windows, it would be Control + N. That creates an empty editor window.
02:34 Let me do a little window management,
02:38
putting the two windows side by side here. So inside the edit window, you’re going to start with a prompt you’ll insert into the input()
function, and the prompt will be pretty simple: "Hey, what's up? "
And again, remember the little space there at the end of it so that you can leave room for prompting. Then you’ll accept that into user_input
.
02:59
user_input
is going to be a variable that you create that is assigned to what is returned from the built-in input()
function. Now, the one thing that’s different here is again, you can put a prompt in. We’ve literally named it prompt
.
03:13
So here’s input()
will prompt with that, and that’ll be assigned, whatever’s typed into user_input
, and then you can print it out. So again, you may want to leave a space because you’ll use your skill of concatenation with the +
(plus sign) there, and then it will print out the user_input
. Great. So again, you’re assigning a string to the literal "Hey,
what's up? "
, using that inside of the input()
prompt to assign to user_input
, and then printing that out.
03:46
So if you were to save—and I’ll just call this simple_prompt
and save it to my desktop.
03:55 Okay. Do you remember how to run programs here? You can use the key command of F5, or you can go to the menu up top that says Run. I’m going to just press F5 and then over here in the interactive window, you’ll see it run. Okay, so there it is.
04:14
It assigned prompt
, the user_input
ran here, and it’s prompting me. So I can say
04:22
Just working
, and pressing Return, it replies it. Great. Pretty simple program, but now you’ve learned how to use user input. What if you use that knowledge to do something a little more interesting, change the functionality a little bit, use some string methods?
04:40
Let’s say you wanted the user input to be shouted. So we’ll change the prompt to say, "What should I shout? "
with the question mark, again leaving the space.
04:50
So it’ll still get accepted into user_input
, it’ll prompt with that. But what we can do is change this a little bit to make it be shouted. You could add a string method. So user_input
, change it to use the .upper()
method, which will make everything uppercase.
05:08 And you could say, you know, something else here if you wanted, but we’ll just keep it simple. So saving that … and running it again with F5. So what should I shout?
05:19
I’ll say happy birthday!
, and it printed it out all caps. Great. And you can use any of the methods that you learned before or string slicing or some other interesting things. All right.
05:34 I think you’re ready to explore user input a little bit deeper on your own. Here’s some review exercises for you. Try writing a program that takes input from the user and then displays that input back, kind of like what you’ve done already, but change it up a bit.
05:51 Try writing a program that takes input from the user and then displays that input in lowercase. Try writing a program that takes input from the user and then displays the number of characters, counting the characters of that input that was typed in.
06:09 All right. Up next, you’re going to start working with strings and numbers.
Become a Member to join the conversation.