Skip to main content

Input/Output

Now that we know how to write code, let's take a look at how to read in user input, and output useful results from our code.

Input

We use the input() function to assign a user's input into a variable.

Whatever the user types into the prompt before pressing [Enter] will be stored into the variable.

input.py
# Get input from the user
# This will be a string of whatever they type into the console BEFORE pressing [Enter]
user_input = input()

Output

We use the print() function to output a message to the user.

The print function can take any data type, and outputs it into the console.

input_output.py
# Get input from the user
# This will be a string of whatever they type into the console BEFORE pressing [Enter]
user_input = input()

print(user_input)
CONSOLE
User input: Hello! Echo!

Output: Hello! Echo!

Additional Resources

Here are some features that you will find useful once you have tried the code above in your own Jupyter Notebook.

tip

Input Prompts

You can prompt the user to input a value by adding a string in the input() function.

input_prompt.py
user_name = input("What is your name? ")
CONSOLE
What is your name? _  <-- User types in their name here
tip

Typed Inputs

What if you want to input a number, or a float?

You will cast your input into the correct type.

input_types.py
user_age = int(input("Enter your age: "))
tip

Common print statements

You can print common strings by writing a string into a print statement.

print("Hello, World!")

You can also add some of your own text before, or after printing a variable.

names = ["Derek", "Timothy"]
list_name = "Elispy Founders"

print("The" + list_name + "are:" + names)
CONSOLE
The Elispy Founders are: [Derek, Timothy]