Python supports following ways to read an input from stdin (standard input),
Python支持以下方式从stdin(标准输入)读取输入 ,
1)使用sys.stdin (1) Using sys.stdin)
sys.stdin is a file-like object on which we can call functions read() or readlines(), for reading everything or read everything and split by newline automatically.
sys.stdin是一个类似于文件的对象,我们可以在其上调用函数read()或readlines() ,以读取所有内容或读取所有内容并自动由换行符拆分。
Example:
例:
from sys import stdin
input = stdin.read(1)
user_input = stdin.readline()
amount = int(user_input)
print("input = {}".format(input))
print("user_input = {}".format(user_input))
print("amount = {}".format(amount))
Output
输出量
123
input = 1
user_input = 23
amount = 23
2)使用input() (2) Using input())
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to string (stripping a trailing newline) and returns that.
如果存在提示参数,则将其写入到标准输出中,而无需尾随换行符。 然后,该函数从输入中读取一行,将其转换为字符串(带末尾的换行符)并返回。
Example:
例:
test = input('Input any text here --> ')
print("Input value is: ", test)
Output
输出量
Input any text here --> Hello Readers!
Input value is: Hello Readers!
Recommended posts
推荐的帖子
Asking the user for integer input in Python | Limit the user to input only integer value
How to get the hexadecimal value of a float number in python?
Convert an integer value to the string using str() function in Python
Convert a float value to the string using str() function in Python
Taking multiple inputs from the user using split() method in Python
翻译自: https://www.includehelp.com/python/how-do-you-read-from-stdin-in-python.aspx