Loading video player…

Reading User Input From the Keyboard With Python (Overview)

You may often want to make your Python programs more interactive by responding dynamically to input from the user. Learning how to read user input from the keyboard unlocks exciting possibilities and can make your code far more useful.

The ability to gather input from the keyboard with Python allows you to build programs that can respond uniquely based on the preferences, decisions, or data provided by different users. By fetching input and assigning it to variables, your code can react to adjustable conditions rather than just executing static logic flows. This personalizes programs to individual users.

The input() function is the simplest way to get keyboard data from the user in Python. When called, it asks the user for input with a prompt that you specify, and it waits for the user to type a response and press the Enter key before continuing. This response string is returned by input() so you can save it to a variable or use it directly.

Using only Python, you can start building interactive programs that accept customizable data from the user right within the terminal. Taking user input is an essential skill that unlocks more dynamic Python coding and allows you to elevate simple scripts into personalized applications.

Download

Course Slides (.pdf)

898.1 KB
Download

Sample Code (.zip)

1.9 KB

00:00 Welcome to Reading User Input from the Keyboard With Python. My name is Christopher, and I will be your guide.

00:07 This is an introductory course to getting a response from your users through the use of the built-in input() function. The input() function returns a string, so if you want to deal with numeric types, you’ll need to convert the response, and in doing that, conversion problems can arrive.

00:24 So this course talks about how to write error-handling code as well. Finally, if you need input but do not want the user’s response on the screen, there’s a method for password-like input that gets covered too.

00:35 The code in this course was tested using Python 3.14. Almost everything in here has been around for a long time though, so any actively supported version of Python will be sufficient.

00:49 Sometimes you want your programs to ask the user for some information. You can prompt them for an answer using the built-in input() function.

00:57 input() returns a string value, so if you need a number, you’ll have to convert it. And conversion comes with problems. You can’t convert "dog" to a number, and users can be tricky about what they type, so you need to know how to handle conversion errors.

01:12 When prompted by input(), the user sees what they’re typing most of the time. That’s what you want. But what about things like passwords? Well, the getpass() function in the module of the same name does not echo key presses to the screen, giving you another choice for getting a response from the user.

01:31 It’s time to get started. In the next lesson, I’ll demonstrate the built-in input() function.

Become a Member to join the conversation.