Read input from stdin in Python: 4 Methods

Python provides you with many functionalities like taking input from the user. Do you want to read input from the user through sdtin then this post is for you. In this tutorial, you get to know the different methods to read input from sdtin.

Method to read input from stdin in Python

Let’s deep dive into the different methods that will know to read input from stdin.

Method 1: Using the input() function

The simplest way to read input from stdin is using the built-in input() function. It allows the user to input and returns a string containing the entered value. Below is an example of it.

name = input("Enter your name: ")
print("Hello, " + name + "!")
input from the user

In this example, the input() function is used to ask the user for their name. The entered value is then stored in the name variable, for future use.

Method 2: Reading multiple inputs

Sometimes, you may need to read multiple inputs from the user in a single line. In such cases, you can use the input() function with appropriate parsing and data manipulation. Here’s an example.

numbers = input("Enter two numbers: ").split()
num1 = int(numbers[0])
num2 = int(numbers[1])
sum = num1 + num2
print("The sum is:", sum)

In this example, the input() function is used to read two numbers from the user in a single line. The input is then split into separate strings using the split() function. After that converted to integers for mathematical operations which is the sum of two numbers.

Method 3: Reading input until a specific condition is met

At times, you may need to read input from the user until a certain condition is met. For example such as reading a list of numbers until a negative value is encountered. For this, you can use a while loop in combination with the input() function. Here’s an example.

numbers = []
num = int(input("Enter a number (negative to stop): "))
while num >= 0:
    numbers.append(num)
    num = int(input("Enter another number (negative to stop): "))

print("You entered:", numbers)

In this example, the user is prompted to enter numbers until a negative value is entered. Each valid input is added to a list, and the final list is displayed after the loop ends.

Method 4: Reading input from a file

In addition to reading input from the user via stdin, Python also allows you to read input from a file. This can be useful when you have a large dataset or when you want to process data stored in a file. Run the below lines of code.

with open("input.txt", "r") as file:
    lines = file.readlines()

for line in lines:
    print(line.strip())

In this example, the open() function is used to open a file named “input.txt” in read mode. Then the readlines() function reads all the lines from the file and stores them in a list. At last, a loop is used to iterate over the lines and print them.

Conclusion

You can easily get input from the user which is an interactive approach. These are the methods to read input from the stdin in Python. The first three methods are very simple. The last method allows you to read the input from the file.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link