Python sys stdin Object

The sys.stdin object in Python's sys module represents the standard input stream. This object is useful for reading input from the user or other input sources, such as files or pipes.

Table of Contents

  1. Introduction
  2. sys.stdin Object Syntax
  3. Examples
    • Basic Usage
    • Reading Input from the User
    • Reading Multi-Line Input
    • Redirecting sys.stdin
  4. Real-World Use Case
  5. Conclusion

Introduction

The sys.stdin object in Python's sys module is a file-like object that provides access to the standard input stream. This object allows you to read input from the user or other input sources in a flexible manner. By default, sys.stdin reads from the keyboard, but it can be redirected to read from other sources as well.

sys.stdin Object Syntax

Here is how you use the sys.stdin object:

import sys

input_data = sys.stdin.read()

Attributes:

  • sys.stdin: A file-like object representing the standard input stream.

Methods:

  • read([size]): Reads at most size bytes from the input stream.
  • readline([size]): Reads a single line from the input stream.
  • readlines([hint]): Reads all lines from the input stream.

Examples

Basic Usage

Here is an example of how to use the sys.stdin object to read input data.

Example

import sys

print("Enter some text:")
input_data = sys.stdin.read()
print(f"You entered: {input_data}")

Reading Input from the User

This example demonstrates how to read input from the user using sys.stdin.readline.

Example

import sys

print("Enter your name:")
name = sys.stdin.readline().strip()
print(f"Hello, {name}!")

Reading Multi-Line Input

This example demonstrates how to read multi-line input using sys.stdin.readlines.

Example

import sys

print("Enter multiple lines of text (end with Ctrl+D or Ctrl+Z):")
lines = sys.stdin.readlines()
print("You entered:")
for line in lines:
    print(line, end='')

Redirecting sys.stdin

This example demonstrates how to redirect sys.stdin to read from a file instead of the keyboard.

Example

import sys

# Open a file and set sys.stdin to read from it
with open('input.txt', 'r') as file:
    sys.stdin = file
    print("Reading from file:")
    file_content = sys.stdin.read()
    print(file_content)

Real-World Use Case

Reading Input from a Script

In real-world applications, the sys.stdin object can be used to read input from a script or command-line tool, making it useful for automation and data processing tasks.

Example

import sys

def process_input():
    print("Processing input data:")
    data = sys.stdin.read()
    lines = data.split('\n')
    for line in lines:
        print(f"Processed line: {line}")

# Example usage
if __name__ == "__main__":
    process_input()

Running the script:

echo "Hello\nWorld" | python script.py

Output:

Processing input data:
Processed line: Hello
Processed line: World
Processed line: 

Conclusion

The sys.stdin object in Python's sys module represents the standard input stream. This object is useful for reading input from the user or other input sources, such as files or pipes. Proper usage of this object can enhance the flexibility and interactivity of your Python programs by allowing them to read input in a variety of ways.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare