Python input() Function: Accept User Input | Syntax, Examples and Use Cases

Introduction: Python input() Function

Many Python programs need to accept information from users while they are running. Whether it’s a name, age, number, password, or any other value, programs need a way to receive input instead of relying on hard-coded data.

Without an input function, programs would be limited to fixed data and could not respond to different user inputs.

The Python input() Function provides a simple and effective solution for this.

What it is: The input() function is a built-in Python function that reads input entered through the keyboard. It displays an optional prompt, waits for the user to enter a value, and returns the entered data as a string, making it easy to build interactive Python programs.

Start with a quick example to understand how the function works.

Then explore its real-world use cases to see where it is commonly used.

Now let’s understand its syntax, parameters, and return value before exploring practical examples.

💡 Tip: The input() function is just one of many useful Python built-in functions. Explore the complete Python Built-in Functions Learning Guide to discover more essential functions, organized by category with tutorials and examples.

Syntax, Parameters, Return Value and Examples: Python input() Function

The following section explains the syntax, parameters, return value and a quick example of the Python input() Function.

Syntax

input(prompt)

Parameters

Parameter Description
prompt (optional) A message displayed to the user before waiting for input. If omitted, the function waits for input without displaying any prompt.

Return Value

Return Value Description
str Returns the value entered by the user as a string, regardless of the type of data entered.

Quick Example

The following example accepts a user’s name and displays a greeting using the input() function.

name = input("Enter your name: ")

print("Hello,", name)


# Sample Output:
Enter your name: Alice
Hello, Alice

The input() function displays the prompt, waits for the user to enter a value, and stores the entered text in the name variable.

How the Python input() function works

  • The input() function optionally displays a prompt.
  • It pauses program execution and waits for the user to enter a value.
  • The user enters the value and presses the Enter key.
  • The entered value is always returned as a string.
  • The returned value can be stored in a variable.
  • Functions like int() or float() can convert the input to numeric data types when needed.

Examples: Python input() Function

The following examples show how the Python input() Function works in different programming scenarios.

Example 1: Reading a User’s Name

name = input("Enter your name: ")

print("Hello,", name)


# Sample Output:
Enter your name: Alice
Hello, Alice

Explanation: The input() function accepts the user’s name as text and stores it in the name variable before displaying it.

Example 2: Reading a Number

age = int(input("Enter your age: "))

print("Age:", age)


# Sample Output:
Enter your age: 25
Age: 25

Explanation: Since input() returns a string, the int() function converts the entered value into an integer.

Example 3: Reading Multiple Inputs

first_name = input("First name: ")
last_name = input("Last name: ")

print("Full Name:", first_name, last_name)


# Sample Output:
First name: Alice
Last name: Johnson
Full Name: Alice Johnson

Explanation: The input() function can be called multiple times to collect different pieces of information from the user.

Example 4: Using a Custom Prompt

city = input("Enter your city: ")

print("City:", city)


# Sample Output:
Enter your city: Hyderabad
City: Hyderabad

Explanation: The prompt provides a clear message so users know exactly what information they need to enter.

Example 5: Reading a Decimal Number

price = float(input("Enter the price: "))

print("Price:", price)


# Sample Output:
Enter the price: 149.99
Price: 149.99

Explanation: The float() function converts the entered string into a floating-point number.

Example 6: Using User Input in a Calculation

number = int(input("Enter a number: "))

print("Square:", number ** 2)


# Sample Output:
Enter a number: 8
Square: 64

Explanation: After converting the input into an integer, the value is used in a mathematical calculation.

Use Cases: When to use the input() Function

Below are some common situations where the Python input() Function becomes useful:

  • Accepting names, numbers, and other information from users.
  • Creating interactive console-based applications.
  • Collecting input for calculations and data processing.
  • Building menu-driven programs that respond to user choices.
  • Testing programs with different user inputs.
  • Developing beginner-friendly projects such as calculators, quizzes, and registration forms.

Key Takeaways: input() Function

Before wrapping up, here are the key points to remember about the Python input() Function:

  • The input() function accepts input from the user through the keyboard.
  • It optionally displays a prompt before waiting for input.
  • The entered value is always returned as a string.
  • Functions like int() and float() convert the input into numeric data types when required.
  • The returned value can be stored in a variable for later use.
  • It is one of the most commonly used functions for building interactive Python applications.

In short, the Python input() Function provides a simple and effective way to accept user input, making Python programs interactive and responsive.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top