read from stdin python in python

To read input from the console (stdin), you can use the input() function in Python 3.x or raw_input() function in Python 2.x. These functions read input as a string until a newline character is encountered.

Here's an example code block to read input from console:

main.py
input_str = input("Enter your input: ")
print("Your input is:", input_str)
75 chars
3 lines

Alternatively, you can use sys.stdin.readline() to read input from the console in a Python script:

main.py
import sys

input_str = sys.stdin.readline().rstrip('\n')
print("Your input is:", input_str)
93 chars
5 lines

In this example code, rstrip('\n') is used to remove the newline character at the end of the input string, as readline() includes the newline character in the returned string.

gistlibby LogSnag