input() reads one line from standard input and returns it as a string without the trailing newline.
name = input("What is your name? ")
print(f"Hello, {name}!")The argument is a prompt. It is optional. If you pass a prompt, ILoveCompiler writes it to output before reading the line.
How standard input works in ILoveCompiler#
ILoveCompiler is not a fully interactive terminal. You provide all input lines before the program starts, using the Input panel in the console.
- One
input()call consumes one line. - A blank line is a valid input value.
- If the program asks for more lines than are available,
input()raisesEOFError. - Prompts are echoed only when you supply a non-empty prompt.
This design keeps runs deterministic and makes exercises easy to test.
Reading numbers#
first = int(input("First number: "))
second = int(input("Second number: "))
print(first + second)With input lines 12 and 30, the output includes the prompts and echoes:
First number: 12 Second number: 30 42
If you need clean output for automated tests, call input() without a prompt:
first = int(input())
second = int(input())
print(first + second)Reading several values from one line#
split() separates a line on whitespace:
width, height = map(int, input("Width and height: ").split())
print(f"Area: {width * height}")If the user enters 3 4, both values are parsed. If they enter a different number of values, unpacking raises ValueError. Validate the count when that matters.
Reading until empty input#
A common pattern reads lines until an empty one:
lines = []
while True:
line = input()
if line == "":
break
lines.append(line)
print(f"Collected {len(lines)} lines")With supplied input, include a blank line at the end to stop the loop.
Practice#
Write a program that reads a name and a favourite number, then prints a sentence using both. Run it once with the prompts included, and once with promptless input() calls to compare the output.
