Write Python. Understand every error.

Starting Python in your browser…

Python basics · topic 6 of 25

Input and output

input() pauses the program and waits for the user to type something. Whatever they type is returned as a string. You can show a message, called a prompt, inside the brackets.

name = input("What is your name? ")
print("Hello,", name)

Because input() always gives text, convert it before doing arithmetic: age = int(input()).

An f-string makes it easy to build a message from values. Put f before the quote, and write variables inside { }. You can also control decimals: {price:.2f} shows exactly two decimal places.

item = "tea"
price = 12.5
print(f"One {item} costs {price:.2f} rupees")

In this app, each line of the Program input box answers one input() call. When your exercise is checked, only what you print() is compared; prompt messages are ignored.

Key takeaways

  • input() waits for the user to type and always returns text, even when they type digits.
  • Convert before doing arithmetic: age = int(input()).
  • An f-string puts values inside text: f"Hello {name}", and {price:.2f} shows exactly two decimals.
  • In Py-CRAFT, each line of the Program input box answers one input() call.

Common mistakes

  • Adding two input() values without converting them. "4" + "6" gives 46, not 10.
  • Typing something that is not a number when the program calls int(input()). Read the ValueError guide.
  • Calling input() more times than there are lines of input. Read the EOFError guide.

Practise with these programs

Share this page:

Ctrl + Enter to run
Line 1, column 1Tab indents 4 spaces. Press Esc then Tab to leave the editor.