How To Read Input In Python

Python is a popular, easy-to-learn programming language that allows developers to create a wide range of applications. One of the fundamental operations that every programmer should know is how to read input from the user, as this enables your program to interact with the user and perform tasks based on their input.

In this tutorial, we will cover different methods of reading input in Python, such as using the input() function, reading from files, and using command-line arguments.

Step 1: Using the input() Function

The most straightforward way to read user input is to use the built-in input() function. This function accepts an optional string parameter, which is used as a prompt to display to the user. When called, input() waits for the user to enter text and then returns that text as a string.

Here’s an example of how to use the input() function:

The input() function always returns a string, so if you need to request a numerical value from the user, you will need to convert the received input to the appropriate type. For example:

In this example, we use the int() function to convert the user’s input into an integer. You can also use float() for floating-point numbers.

Step 2: Reading Data from a File

Another common way to read input in Python is by reading data from a file. To do this, you will need to use the open() function to open the file and create a file object. Once the file is opened, you can use methods such as read(), readline(), and readlines() to read the contents of the file.

Here’s a simple example of how to read data from a text file called “example.txt”:

In this example, the "r" parameter passed to the open() function tells Python to open the file in read mode. The read() method is called on the file object to read the contents of the file as a single string. Finally, we close the file using the close() method.

A more Pythonic way to read a file is by using the with statement, which takes care of closing the file for you:

Step 3: Parsing Command-Line Arguments

Sometimes, you may want to read input from the command line when your Python script is executed. To do this, you can use the sys.argv list, which contains the command-line arguments passed to the script.

For instance, here’s an example of a Python script that prints the sum of two numbers provided as command-line arguments:

In this example, we import the sys module to access the argv list, which contains the script name and any command-line arguments. We ignore the script name itself (contained in sys.argv[0]) and convert the arguments to integers before performing the addition.

To execute this script, save it as “” and run it from the command line:

This will output:

The sum is: 8


In this tutorial, we have covered three methods to read input in Python: the input() function for user prompts, reading data from files, and parsing command-line arguments using the sys.argv list. Now you should be able to incorporate different types of input into your Python programs, allowing for more interactive and dynamic applications.