Requesting Input
00:00
In the previous lesson I gave an overview of the course. In this lesson, I’ll show you how to use the built-in input() function.
00:07
Built-in functions are those that you can use directly in Python without having to import them first. input() is one such built-in function. It asks the user for a response and then waits for them to type something in.
00:20
input() takes a single argument, a string that the function shows as a prompt to the user. The return value of input() is also a string, even if the user types in numbers.
00:32 In a future lesson, I’ll show you how to deal with that, but for now, let’s stick with the string responses.
00:38 For about half of this course, I’ll be demonstrating code in the Python REPL. If you haven’t come across that term before, REPL stands for Read–Eval–Print loop.
00:48 It’s an interactive Python session where it reads what you type, evaluates it as a Python statement, prints the result of the evaluation to the screen, and then prompts you for the next thing, looping back. Your IDE likely has a REPL built in, or you can run one by invoking Python on the command line without any arguments.
01:07 Once you run it, you’ll be prompted with three greater-than symbols. That’s where you can type some Python and see the result.
01:15
For example, if you call print(), you’ll see the printed value. I’m going to start a REPL session right now to demonstrate the input() function.
01:24
Since the input() function is built in, I don’t need to import anything to use it. I can just call it directly. So let me do just that.
01:34
input() takes a string as an argument. The string you give it is a prompt for the user. When I hit Enter here, Python prints the prompt and waits for my response.
01:44 “What is your name?” is the prompt,
01:47 and there I’ve typed a response. When I hit Enter, the REPL shows the return value from the function call, which in this case is a string containing what I typed, the name “Vincent”.
01:59 Note that I’m using Python 3.14, which added colorization to the REPL. It knows what a prompt line is and colors it in bold magenta. If you were running the same code in a program or in an older version of the Python REPL, you’d only get plain text. Later in the course, when I’m using a script, this won’t be colorized.
02:19
Unless you’re playing around in the REPL, you typically want to capture the response of the input() function. Let me run this again, but this time store the result.
02:33 Similar code to before, the same prompt, and the same answer. This time the REPL shows nothing. When you assign something in the REPL, nothing gets returned, so there’s nothing to evaluate, which means the REPL doesn’t print anything out.
02:49
It did, however, store our answer in the variable name. You can evaluate the content of a variable simply by entering it into the REPL.
02:57
Typing in the variable name causes the REPL to evaluate it and print its contents. Since it is a variable, I can also show it on the screen by calling the print() function.
03:09
print() can take more than one argument. Here I’ve given it two. The first argument is the string “Hello”, while the second is our name variable. print() then combines all of its arguments, putting a space between each of them, and outputs the result to the screen like you see here.
03:26
Note a subtle difference between printing the name and evaluating it. When you evaluate the name variable, the REPL puts quotes around it because it’s a string, whereas print() outputs the contents to the screen, so there’s no quotes.
03:39 If you want to do fancier things with your output, you might want to use an f-string. An f-string is a way of constructing a new string where you can embed the evaluation of variables inside of it.
03:50
You denote an f-string with the letter f as a prefix, and you indicate the variables you want to evaluate in the string with brace brackets. Let me print one out.
04:03
Although the output here is the same as above, I constructed it differently. In the first version of the print(), there was a string and a variable.
04:10
In the second version, there’s only an f-string. Inside the f-string, name gets evaluated because it’s surrounded in brace brackets, so the interpreter’s evaluation of the f-string is a single string containing “Hello Vincent”, all of which is then handed to print(), which print() then prints out, giving us the same thing as above.
04:29 In this case, you could use either method I’ve shown you to get to the same place, but f-strings are fairly powerful and allow for formatting and other cool things.
04:38
If you haven’t played with them before, I highly suggest you put an f-string course on your to-do list. So far, I’ve been calling input() with a prompt argument.
04:47 You can also call it without one.
04:50 The user still gets to enter something, only the prompt is empty, meaning it’s a blank line. Let me type something in, and like before, the response is a string, which the REPL displayed.
05:04
The first time you see, there was no prompt, was me typing. The second time, it’s in quotes. That’s the return result of the input(), the string containing “There was no prompt”.
05:15
I don’t recommend using input() without an argument, as it likely will confuse your users. But hey, you might have a special case where you want no prompt.
05:25
The input() function always returns a string, so if you want numbers as input, then you have to convert the response. I’ll show you how to do that in the next lesson.
Become a Member to join the conversation.
