5. Input function

input() is a built-in function that reads one line typed by the user. Its signature is:

  • prompt — text shown before reading. Default: nothing.
  • It always returns a string, even when the user types digits.

The prompt is what the user sees, so use it to say what you are asking for. Once you have the answer, use int() or float() to turn it into a number.

This course runs in the browser, so each exercise provides the lines the user would type. input() prints the prompt, reads a line and the console echoes it, just like a terminal. The exercise card shows an editable Input field with those lines.

#01

Ask for a name

Call input("Name: ") to show the prompt and read the next line. Assign the answer to name, then print it.

The simulated user types Ada after the prompt, so the console shows the prompt, the typed line and then the program output.

Print exactly:

The program reads these lines when you click Check.

Output
Run your code to see the output here.
#02

Greet the user

Read the name with the prompt "Name: ", then print "Hello, " followed by the answer.

The simulated user types Ada after the prompt.

Print exactly:

The program reads these lines when you click Check.

Output
Run your code to see the output here.
#03

Convert the answer

input() returns text, so input() + 1 would fail. Wrap it in int() first. Read the age with the prompt "Age: ", convert it, and print the age plus one.

The simulated user types 20 after the prompt.

Print exactly:

The program reads these lines when you click Check.

Output
Run your code to see the output here.
#04

Read two lines

Each call to input() reads the next line. Read a first name with the prompt "First: ", read a last name with the prompt "Last: ", then print both separated by a space.

The simulated user types Ada and then Lovelace.

Print exactly:

The program reads these lines when you click Check.

Output
Run your code to see the output here.
#05

Skip the prompt

The prompt is optional. Call input() with no prompt, assign the answer to name, then print "Hi, " followed by the name. Without a prompt the program prints nothing before reading, but the terminal still echoes the line the user types.

The first Ada is the echoed input; the second line is your print().

Print exactly:

The program reads these lines when you click Check.

Output
Run your code to see the output here.