Coding for All
Lesson 7: Asking for Input
Asking for Input Ask the user a question and use their answer.
1name = input("What is your name? ")
2print(f"Nice to meet you, {name}!")
Terminal
$ python3 main.py
What is your name? Ada
Nice to meet you, Ada!
Every line, explained
name = input("What is your name? ")
input(...) shows the question in the terminal, then waits for the user to type an answer and press Enter. Whatever they type comes back as a string and is stored in name. (In our pretend terminal, someone called Ada answers.)
print(f"Nice to meet you, {name}!")
The f right before the opening quote makes this an f-string. Inside an f-string, curly braces are magic: Python swaps {name} for the value of the variable name. This is the modern Python way to mix variables into text. The answer the user typed is now part of the message.