CodeBlanks Logo Icon CodeBlanks

input()

Learn to read a line with input().

Get input with input()

The input() function reads one line from the user and returns it as a string. It takes an optional message argument shown on screen:

name = input("Ad: ")
# Ad: Ayşe  (user types)
print(name)  # Ayşe

input() always returns a string — if you need a number you must convert it afterwards. An empty line returns an empty string "".

It is the basic tool for scripts that read data from the command line.

Message and empty input

The message passed to input() is shown to the user, then a line is read. If the user presses Enter without typing, an empty string is returned:

answer = input("Continue? ")
# user just presses Enter
print(repr(answer))  # ''

The message argument can be omitted: input() just waits. The message tells the user what to enter; a good message improves input quality.

Use clear messages in interactive scripts.

Practice Greet with input

Assign input("Ad: ") to name. The user types Ayşe. Then print the greeting with print("Merhaba, " + name).

Starter code

main.py
# ask for a name with input
# then greet

Sample solution

main.py
name = input("Ad: ")
print("Merhaba, " + name)

Expected output

Ad: Merhaba, Ayşe

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does input() return?

  • A boolean
  • An integer
  • A list
  • A string Correct answer

Explanation: input() always returns a string; for numbers you need int() or float().

2 Complete the input call

Question
name = ____
Answer
name = input("Ad: ")

Explanation: input("Ad: ") shows the message and returns the entered line.

3 Match input concepts

Term Meaning
input() Reads input
message Shown to user
"" Empty input result
string Return type

All lessons in Beginner