Course contentsShow
← Python
Lesson 14 of 1,0601. Foundations and EnvironmentFree lesson

Handling User Input and Output

You will learn how to capture data from the user and display results back to the console using input and print functions.

In this lesson, you will learn how to make your programs interactive by capturing data from a user and displaying information back to them.

So far, your programs have been static—the values were hardcoded by you. To make a program dynamic, it needs to talk to the user. We do this using two built-in functions: input() and print().

The input() Function

The input() function pauses your program and waits for the user to type something on their keyboard. Whatever the user types is captured as a string of text. You usually want to save this data into a variable so you can use it later. You can also include a "prompt" inside the parentheses to tell the user what to type.

user_name = input("What is your name? ")

The print() Function

Once you have that data, you need a way to show it back to the user. The print() function displays information to the console. You can pass it variables, literal text inside quotes, or a combination of both separated by commas.

# Combining text and variables
print("Hello,", user_name)

Putting It Together

Imagine you are building a simple greeting tool. Here is how you capture the user's name and say hello:

# 1. Ask the user for their name
name = input("Please enter your name: ")

# 2. Display a personalized greeting
print("Welcome to Python programming, " + name + "!")

When this code runs, the program prints the prompt, pauses until you press the "Enter" key, and then proceeds to print your personalized message. By combining these two functions, you can create programs that respond uniquely to every person who uses them.

Key Takeaway: The input() function captures data from the user as text, while the print() function displays text or variable values to the console, allowing for two-way communication in your scripts.