Lesson 8 of 10
So far, every value in your programs has been hardcoded. input() lets your program ask the
person running it for information.
name = input("What's your name? ")
print(f"Hello, {name}!")
input() does two things: it displays the message you pass it (the prompt), pauses the
program, and waits for the user to type something and press Enter. Whatever they typed is
returned as a string.
In this browser: since there's no terminal here,
input()in the exercises below opens a small popup asking for the value. In a normal terminal, it would simply wait for you to type and press Enter on the same line as the prompt.
input() always returns a string
This trips a lot of beginners up. Even if the user types a number, input() gives you back a
string — you need to convert it if you want to do math with it (remember lesson 1.5!).
age_text = input("How old are you? ")
age = int(age_text)
print(f"Next year you'll be {age + 1}.")
A common shortcut is to convert on the same line:
age = int(input("How old are you? "))
print(f"Next year you'll be {age + 1}.")
Combining input with f-strings
name = input("Name: ")
city = input("City: ")
print(f"{name} lives in {city}.")
Try it yourself
Ask the user for their name with input(), then print a greeting using an f-string.
Checkpoint
input(prompt)displaysprompt, waits for input, and returns it as a string.- Convert the result with
int()orfloat()if you need to do math with it. input()pauses program execution until the user responds.
Questions about this lesson
Log in to ask a question.