🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

input()

AI & DATA SCIENCE // input

input() displays an optional prompt, pauses execution, and reads a single line of text typed by the user, always returning it as a string.

Syntax

input(prompt='')

Deep Dive Course

input() writes its prompt argument, if given, to standard output without a trailing newline, then blocks until the user types something and presses Enter, stripping the trailing newline from what they typed and returning the result always as a string, even if the user typed digits. That's why numeric input needs an explicit conversion, and why that conversion can raise ValueError if the text isn't a valid number.

1Understanding input()

input() writes its prompt argument, if given, to standard output without a trailing newline, then blocks until the user types something and presses Enter, stripping the trailing newline from what they typed and returning the result always as a string, even if the user typed digits. That's why numeric input needs an explicit conversion, and why that conversion can raise ValueError if the text isn't a valid number.

💡

Always wrap a numeric conversion of input() in a try/except ValueError block — real users will eventually type something that isn't a number.

editor.html
name = input("What is your name? ")
print(f"Hello, {name}!")
localhost:3000

2Practical Example

Here is a real-world application of input() showing how it is used in production Python code.

editor.html
while True:
    text = input("Enter your age: ")
    if text.isdigit():
        age = int(text)
        break
    print("Please enter digits only.")
print(f"You are {age} years old.")
localhost:3000

3Best Practices

Follow these guidelines when working with input():

1. Convert input() with int() or float() explicitly when you need a number — it's always a string otherwise

2. Validate input in a loop, asking again, rather than crashing on the first bad entry

3. Avoid using input() in automated/non-interactive scripts, like web servers — it will hang waiting for a terminal that isn't there

⚠️

Tip: Always wrap a numeric conversion of input() in a try/except ValueError block — real users will eventually type something that isn't a number.

editor.html
name = input("What is your name? ")
print(f"Hello, {name}!")
localhost:3000

Examples

Example 01Basic Usage
name = input("What is your name? ")
print(f"Hello, {name}!")
Example 02Advanced Example
while True:
    text = input("Enter your age: ")
    if text.isdigit():
        age = int(text)
        break
    print("Please enter digits only.")
print(f"You are {age} years old.")

Best Practices

  • Convert input() with int() or float() explicitly when you need a number — it's always a string otherwise
  • Validate input in a loop, asking again, rather than crashing on the first bad entry
  • Avoid using input() in automated/non-interactive scripts, like web servers — it will hang waiting for a terminal that isn't there

Interview Question

What type does input() always return, and why can converting it with int() raise an exception?

Hint: Think about what the user could type.

input() always returns a string, regardless of what the user typed — even a numeric-looking entry comes back as text, not a number. Wrapping it in int() attempts to parse that text as a whole number; if the user types anything that isn't a valid integer literal, like letters, a decimal point, or empty input, int() raises a ValueError, so real programs should catch that and re-prompt instead of crashing.

Exercises

MediumPractice using input() in a real scenario.
View Solution
name = input("What is your name? ")
print(f"Hello, {name}!")

Frequently Asked Questions

What type does input() always return, and why can converting it with int() raise an exception?

input() always returns a string, regardless of what the user typed — even a numeric-looking entry comes back as text, not a number. Wrapping it in int() attempts to parse that text as a whole number; if the user types anything that isn't a valid integer literal, like letters, a decimal point, or empty input, int() raises a ValueError, so real programs should catch that and re-prompt instead of crashing.

Related Functions

print()int()str.isdigit()