您如何从Python的stdin中读取信息?
Python支持以下方法从stdin(标准输入)读取输入,
1)使用sys.stdin
sys.stdin是一个类似于文件的对象,我们可以在其上调用函数read()或readlines(),以读取所有内容或读取所有内容并自动由换行符拆分。
示例
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))输出结果
123 input = 1 user_input = 23 amount = 23
2)使用input()
如果存在提示参数,则将其写入到标准输出中,而无需尾随换行符。然后,该函数从输入中读取一行,将其转换为字符串(将尾随换行符分隔)并返回。
示例
test = input('Input any text here --> ')
print("Input value is: ", test)输出结果
Input any text here --> Hello Readers! Input value is: Hello Readers!
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短