Pyrthon User Input

Python 6 min min read Updated: Mar 09, 2026 Beginner
Pyrthon User Input
Beginner Topic 8 of 10

User Input in Python

User input allows a Python program to receive data directly from the user while the program is running. This interaction makes programs dynamic and useful in real-world applications such as calculators, login systems, form processing, and command-line tools.

Python provides a built-in function called input() to collect data from the user through the keyboard.

What is the input() Function?

The input() function is used to take input from the user. When the program reaches this function, it pauses and waits for the user to enter a value. Once the user enters the value and presses Enter, the program continues execution.

python name = input("Enter your name: ") print("Hello", name)

In this example, the program asks the user to enter their name and then prints a greeting message.

Input Always Returns a String

By default, the value returned by the input() function is always treated as a string, even if the user enters numbers.

python age = input("Enter your age: ") print(type(age))

The output will show that the data type is str (string).

Converting User Input to Numbers

If you want to perform mathematical operations on user input, you need to convert the input into numeric data types such as int or float.

Using int() for Integer Input

python age = int(input("Enter your age: ")) print("Your age next year will be:", age + 1)

Using float() for Decimal Input

python price = float(input("Enter product price: ")) print("Price after tax:", price * 1.18)

Here, the input is converted into numeric values so arithmetic operations can be performed.

Taking Multiple Inputs

Python allows users to input multiple values in a single line and then split them into separate variables.

python name, age = input("Enter name and age: ").split() print("Name:", name) print("Age:", age)

The split() function separates the input values based on spaces.

Taking Multiple Numeric Inputs

You can also convert multiple inputs into integers using map().

python a, b = map(int, input("Enter two numbers: ").split()) print("Sum:", a + b)

This program allows the user to enter two numbers and calculates their sum.

Real-World Example

User input is widely used in real-world applications such as calculators and form-based programs.

python num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) sum_result = num1 + num2 print("Sum of numbers:", sum_result)

This program takes two numbers from the user and prints their sum.

Handling Invalid Input

Sometimes users may enter incorrect data types, which can cause errors. In such cases, error handling can be used to prevent program crashes.

python try: number = int(input("Enter a number: ")) print("You entered:", number) except: print("Invalid input! Please enter a number.")

This program prevents errors if the user enters invalid data.

Best Practices for User Input

  • Always display a clear message when asking for input.
  • Convert input values to the correct data type when necessary.
  • Use error handling to prevent crashes.
  • Validate user input when building real-world applications.

Conclusion

User input is an important feature in Python that allows programs to interact with users. By using the input() function along with type conversion functions such as int() and float(), developers can create dynamic and interactive applications.

In the next tutorial, we will learn about Functions in Python and how to organize reusable blocks of code.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators