Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Reading Input From the Keyboard

00:01 In this first lesson, you will learn how to read input from the keyboard, and you will do that using the input() function. This input() function allows you to enter something with the keyboard and then use it inside of your Python script.

00:14 Let’s head back over to a Python script. I have here an empty script. There’s nothing in here so far, but I’m going to make a comment for myself. I’m just going to say that I want to collect user input. And in Python, you can do this using the input() function.

00:31 It works just like this, that you type the word input and then open-close the brackets. This indicates a function call. And that’s enough. This instructs Python to stop execution, pause it, collect the input from the user, and continue only after the user pressed Enter. Let’s give this a spin.

00:48 I’m opening up a terminal and now I’m going to run this script. I’m going to say python script.py, press Enter, and you see that Python stops execution here.

00:59 It doesn’t jump back into my terminal because you’re still inside of the Python program right now. You’re sitting inside of input(), that opened up a portal for you to type some text. So now I can say hello, and once I press Enter, only then the script continues.

01:14 And because there’s nothing that’s going in here, it exits and goes back to my Bash console.

01:20 Now, this instruction isn’t very descriptive, so something you can do in here is you can add a string that describes what you want the user to input here. So, for example, I can say, "What's your name: ",

01:33 and then if you run this again,

01:36 then you will see that the prompt displays here inside of your console, and that makes more sense. As a user, I know what I’m supposed to do, so I can type in here my name. I’m going to say Martin, press Enter. Python continues, and because nothing else goes on in this small script, it exits and goes back to the Bash console.

01:56 Now, there’s not much happening with this, but the input() function returns whatever the user inputs here. So in this case, that would be the string "Martin".

02:05 You can catch the input by assigning it to a variable. You can say name = input(), and then when I run this script again and input my name, it’s going to be stored inside of the script under the name variable.

02:19 Unless you do something with that name variable, not much is going to happen. But like this, you can save it and then reuse it in your script.

02:27 If I run this again, you won’t see a difference because currently I’m not doing anything with the name variable, but now, for the moment that this script exists before it exits again, my name "Martin" here exists as a string in the name variable.

02:43 And that’s all for collecting input. You really just need to put the input() function, you can optionally pass it a prompt, and then Python is going to pause execution, collect whatever you type in there until you press Enter, and optionally you can save it to a variable name, which makes a lot of sense if you want to do something with the user input. Now in the next lesson, you will see something that you can do with an input like that, and also a potential trouble that you can run into and how to solve it.

Become a Member to join the conversation.