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.
# 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.
# 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)
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.
Input Prompts
You can prompt the user to input a value by adding a string in the input()
function.
user_name = input("What is your name? ")
What is your name? _ <-- User types in their name here
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)
The Elispy Founders are: [Derek, Timothy]