๐Ÿ Lesson 5: Getting Input

Make your programs talk back with input()

โ† Back to Python Basics

๐ŸŽค The input() command

input() pauses your program and waits for the user to type something and press Enter. Whatever they type is handed back as a string, which you usually save in a variable.

name = input("What is your name? ") print(f"Nice to meet you, {name}!")

The text inside input("...") is the prompt โ€” the question shown to the user.

๐Ÿ–ฅ๏ธ In this playground

When your code reaches input(), a small pop-up box appears. Type your answer there and press OK. Try it in the Practice tab!

โš ๏ธ input() always gives you text

Numbers from input are strings!

Even if the user types 7, input() returns the string "7". To do math, you must convert it with int() or float().

age = input("How old are you? ") age = int(age) # convert text to a number print(f"Next year you'll be {age + 1}")

You can even do it in one line:

age = int(input("How old are you? "))

๐Ÿงช A complete mini-program

name = input("Your name? ") fav = input("Favorite food? ") print(f"{name} loves {fav}! Yum ๐Ÿ˜‹")

โœ… What you learned

  • input("prompt") waits for the user to type something.
  • It always returns a string.
  • Wrap it in int() or float() to get a number.