So far, your Python programs have been a one-way street. We can print() information out to the user, but how can we build an interactive program that gets and uses input from the user? Input data can come in many forms. You may want the user to enter their name or answer simple questions in text format. Programs can also get input by reading data from a file, recording audio or visual information from webcams and microphones, and so on.

In this lesson (and throughout the course), we'll focus on getting text input from the user. Text input can be easily typed into a console window and does not require a graphical interface or any special hardware to record audio or visual information. Python provides a handy input() function that allows the user to enter a piece of text information using their keyboard.

The input() Function
You can call the input() function to ask the user a question and get a string answer. The input() function has the following form:

variableName = input(question_for_user)
Copy
This function is very simple but very powerful! The input() parameter is a string that contains a question you want to ask the user. When the input() function runs, it will pause your program and wait for the user to type in an answer and hit "Enter" on the keyboard. The user's text response is returned, and you can store that value in a string variable or use it in some other string expression.

The following example asks the user to enter their name and stores the result in the userName variable. We then print() the userName variable out to the screen to show what the user entered. This is the first time you've run an interactive program in our live code tester, and the "Console" area will now allow you to type in a response. Try it yourself!

Try It Now


When the program runs, it will print out the input question and wait for you to enter a response. Click your mouse in the "Console" area, type in your name, and press "Enter". The value that you enter is stored in the userName variable and then printed on the screen.

Take a close look at the prompt message we display to the user - "Please enter your name: ". Notice it has a colon and a space at the end. The input() function won't add any sort of spacing or separation automatically. So, if you don't add these yourself, the text entered by the user will appear directly after the end of the message without any spacing.

As you can see, input data will often help produce the program's output. The data a user enters may be processed to produce specific output (like our welcome message). User inputs may also be used to select features or processes to run, guiding the program behavior into producing other desired output.
input() String Results
The input() function will always return a string value. So, if you ask the user for his or her age and get "21" as a result, that value is "21" as a string and not 21 as a number. Try the example below. It works when you enter a numeric age for the second input() question, but it will also work when you enter something other than a number like "blue".

Try It Now


You might want the user to enter a numeric value and prompt the user to enter a number. But, the user can and might enter any sort of data. All user input is returned as a string.

Please enter your name: Maximus
Welcome Maximus
How old are you? blue
You are blue years old
Converting Numeric Input
If your program really needs numeric input, what can you do? Python provides a couple of handy functions that will turn a string value into a numeric data type - either an integer or a floating-point value. Use the int() function to convert a string to an integer or the float() function to convert a string to a floating-point value.

The example below gets a string value, converts it to an integer with int(), does some math on the resulting numeric value, and then makes use of the numeric result.

Try It Now


Try running this code three times. The first time, enter a valid integer like 16. The second time, enter a floating-point number like 16.5. Finally, enter a nonsense answer like "blue". What happens when you enter invalid data that cannot be stored in an integer? You should see an error message like the one below.

How old are you? 16.5
Traceback (most recent call last):
File "code1.py", line 2, in <module>
userAgeInt = int(userAgeString) # convert to an integer
ValueError: invalid literal for int() with base 10: '16.5'
The error means that you are trying to convert a value that is not actually an integer to an integer data type. Later in the course, we'll show you how to verify user input and avoid this kind of error. For now, we'll rely on the user to enter the correct kind of data when prompted by a program.

Event-Driven Programming
In console applications (like the ones we are creating in this course), the program flow starts at the beginning of the program and executes each statement in a predictable manner until the program ends. The program will pause at input() statements, waiting for the user to finish entering data. When the user hits "Enter", this event causes data to be sent back to the program, where normal flow continues.

A user-generated event, like hitting the "Enter" key on the keyboard, is a signal a program can use to do something like read input data, move to the next screen, or run a command. Pressing keys on a keyboard is one simple example of an event, but programs can respond to other kinds of user input as well.

In programs with a graphical interface, a user may be faced with a screen that has many different options on it. The screen may hold a variety of buttons, text boxes, list boxes, and other controls that the user can select at any time. There is no way to predict exactly which action the user will choose to perform. Instead of executing from start to finish in an orderly manner, your program must respond as the user takes actions like clicking on a control with a mouse. Graphical programs are event-driven, meaning certain functions will execute when specific events occur.

Programs may even receive events from other programs! Think about what happens when a web browser running on your computer requests a web page from a web server. There is some time delay between the request from your browser and the data returned from the server. The arrival of data from a network connection is a kind of event that can trigger processing in an application. Your web browser will display the resulting web page once the data has arrived.

1. What is the purpose of the input() function?
2. What one parameter do you provide to input()?
3. What kind of data does input() return? How do you store and use that data?
4. How can you convert a string to an integer or floating-point value?
5. What may happen if the user enters unexpected input that you try to convert to a number?

1. The purpose of the input() function is to get text input from the user.

2. The parameter you provide to input() is the question or prompt you want to ask the user.
3. The input() function returns a string value. You can store and use that data by assigning it to a variable and using that variable in your program.
4. You can convert a string to an integer by using the int() function and a string to a floating-point value by using the float() function.
5. If the user enters unexpected input that you try to convert to a number, it may raise a ValueError.