Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we will learn how to take user input in Python. The function we use is called input(). Can anyone tell me what kind of data we can input using this function?
Is it just strings?
Great point! The input function returns data as a string, regardless of what the user types in. For example, if I ask for your name, I might do it like this: name = input("Enter your name: "). Now, can someone tell me how we might display a welcome message using the name we just captured?
We can use the print() function for that!
Exactly! We would use it like this: print("Welcome, " + name). This will concatenate and display a personalized message. Always remember, print() is for output and input() is for input.
So it's like one gets data and the other shows it?
Exactly! To help you remember: 'Input' starts with an 'I' for 'In' and 'Output' starts with an 'O' for 'Out'.
Now that we know how to take input, let’s focus more on how to display output. print() can do a lot more than just displaying static messages. What do you think happens if we want to include numbers or calculations in our output?
Do we need to convert them to strings before printing?
Good thinking! Yes, you do need to convert numbers or concatenate them as strings. For example, if we wanted to show someone their age, we could do something like this: age = 20 and then print("You are " + str(age) + " years old").
What if we just want to output a calculation result?
In that case, you don’t have to convert since the print function can directly output the result of expressions. For example, print(10 + 5) would simply give us 15 in the console. Quick quiz: What will print("Your total is: ", 10 + 5) display?
It should say 'Your total is: 15'!
Right! Keep in mind that print() can take multiple arguments, separated by commas.
Input from users, then output the news, print it with style, make learning worthwhile!
Imagine a friendly robot named Inputter who loves to ask questions and a magician named Printer who loves to share messages. Together, they make coding interactive and fun!
I - Input, U - User, O - Output, D - Display: 'I U O D' to remember that Input is for taking data and Output is for displaying it.
I/O: Input/Output; it's like asking a friend (I) and then telling another friend (O) about what was said.
Term
What does the input() function do?
Click to flip
Term
What is the purpose of the print() function?
Click to flip
Term
How do you concatenate a string with a number in print()?
Click to flip
This section explains how to take user inputs and display outputs in Python using the input() and print() functions.
In this section, we explore the basic methods of receiving user input and displaying output in Python programming. We learn how to use the input() function to get data from users, and the print() function to present information back to them.
Input and output are fundamental aspects of programming that allow users to interact with the program. In Python, we primarily use two functions for this task: input() and print().
Input: To receive data from users, we use the input() function. It prompts the user for input and stores that input as a string variable. For example, name = input("Enter your name: ") will display a prompt asking the user to enter their name and will store their response in the variable name.
Output: To display information to users, we use the print() function. This function outputs whatever content is provided to it in a user-friendly manner. For instance, print("Welcome to Python") will display the message "Welcome to Python" in the console.
Understanding these two functions is crucial for making interactive programs that respond to user behavior, making them more dynamic and enjoyable.