Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

input: reading one line of text

input reads a line from standard input and returns a string without its final newline. It does not automatically turn typed digits into a number.

Version: Python 3.14

Syntax

input()
input(prompt, /)

In an ordinary program, input("Name: ") first writes a prompt without a newline, then waits for input. The result is a str, even when the user enters digits only. Reading an empty line produces the empty string "".

Convert separately, for example with int(text), and handle ValueError when the text is not a valid integer. input() removes the final newline, but does not generally clean up whitespace; using strip() is a separate decision that depends on the data.

Reaching the end of available input without another line raises EOFError. This differs from an intentionally entered empty line. Do not use eval to turn a user response into a number: it would execute the input as code.

The example substitutes StringIO for standard input to demonstrate an empty line, a number and end of data without waiting for a keyboard. The finally block restores the previous stream. In an interactive lesson, use the input field supplied by the environment.

Examples

import sys
from io import StringIO

previous = sys.stdin
try:
    sys.stdin = StringIO("\n42\n")
    empty = input()
    number = int(input())
    print(repr(empty), number)
    try:
        input()
    except EOFError:
        print("EOFError")
finally:
    sys.stdin = previous

Result

'' 42
EOFError

Sources