欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

您如何从Python的stdin中读取信息?

发布时间:2025/3/11 python 28 豆豆
生活随笔 收集整理的这篇文章主要介绍了 您如何从Python的stdin中读取信息? 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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 stdininput = 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 = 23amount = 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

总结

以上是生活随笔为你收集整理的您如何从Python的stdin中读取信息?的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。