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
推薦的帖子
Read input as an integer in Python
在Python中將輸入讀取為整數
Read input as a float in Python
在Python中以浮點形式讀取輸入
Parse a string to float in Python (float() function)
解析要在Python中浮動的字符串(float()函數)
Asking the user for integer input in Python | Limit the user to input only integer value
要求用戶在Python中輸入整數| 限制用戶僅輸入整數值
Asking the user for input until a valid response in Python
要求用戶輸入直到Python中的有效響應
Input a number in hexadecimal format in Python
在Python中以十六進制格式輸入數字
Input a number in octal format in Python
在Python中以八進制格式輸入數字
Input a number in binary format in Python
在Python中以二進制格式輸入數字
How to get the hexadecimal value of a float number in python?
如何在python中獲取浮點數的十六進制值?
Convert an integer value to the string using str() function in Python
使用Python中的str()函數將整數值轉換為字符串
Convert a float value to the string using str() function in Python
使用Python中的str()函數將浮點值轉換為字符串
Input and Output Operations with Examples in Python
使用Python中的示例進行輸入和輸出操作
Taking multiple inputs from the user using split() method in Python
使用Python中的split()方法從用戶獲取多個輸入
Fast input / output for competitive programming in Python
快速輸入/輸出,可在Python中進行有競爭力的編程
Precision handling in Python
Python中的精確處理
Python print() function with end parameter
帶有結束參數的Python print()函數
翻譯自: https://www.includehelp.com/python/how-do-you-read-from-stdin-in-python.aspx