Python input() - 从控制台读取用户输入

位置:首页>文章>详情   分类: Python教程 > 编程技术   阅读(353)   2024-05-15 10:57:08

Python 输入() 函数允许用户在程序中输入。 输入()函数从输入中读取一行,转换为细绳并将其作为方法返回值返回。

输入() 函数可选地允许将提示字符串作为在接受输入时显示给用户的方法参数。

示例:Python 输入() 方法从控制台获取用户名

在给定的示例中,当在控制台中询问时,我输入了我的名字“Lokesh”。

x = input()
print('Hello, ' + x)

程序输出。

Hello, Lokesh

Syntax of 输入() Method

输入()方法的语法是:

input([prompt])

方法参数

快速的 (选择修改的):一个字符串,表示标准输出(通常是屏幕)中输入前的消息。提示消息后没有尾随换行符。

方法返回值

输入() 方法读取用户在控制台输入的一行。它通过删除尾随的换行符将行转换为字符串并将其返回给程序。

如果我们尝试读取 EOF,它会抛出一个 EOFE 错误 异常。

Python 输入() Method Example

示例 1:在没有提示消息的情况下读取用户输入

userInput = input()

print('The input string is:', userInput)

程序输出。

cundage
The input string is: cundage

示例 2:在没有提示消息的情况下读取用户输入

inputNumber = input("Enter the number to get the square: ")

print('The square of {0} is: {1}'.format(
inputNumber, int(inputNumber) * int(inputNumber)))

程序输出。

Enter the number to get the square: 5
The square of 5 is: 25

快乐学习!!

地址:https://www.cundage.com/article/python-input.html

相关阅读

Python 范围 type represents an 不可变的数字序列. The most common use of a range is to use it for iterating ...
Learn to use Python 打印() function to 将 Python 程序或 Python 脚本的打印输出重定向到文件. 1. 使用 文件 参数打印到文件 打印()
Python ascii() 方法是语言内置函数,它返回一个包含对象的可打印表示形式的字符串。 ascii() 转义字符串中的非 ASCII 码 字符使用\X、\u 或 \U 转义。 1.语法 ...
Python 范围 type represents an 不可变的数字序列. The most common use of a range is to use it for iterating ...
Python ascii() 方法是语言内置函数,它返回一个包含对象的可打印表示形式的字符串。 ascii() 转义字符串中的非 ASCII 码 字符使用\X、\u 或 \U 转义。
Python print() 函数将给定的字符串或对象打印到标准输出。标准输出是屏幕或文本流文件。 示例:将整数打印到屏幕 print("Learning Python is Easy.")...
Python bin() 方法将给定的 integer 转换为等效的二进制 < href="https://howtodoinjava.com/python-datatypes/pytho...
在 Python 中,string.count() 用于计算给定输入字符串中字符或子字符串的出现次数。 input_string = "how to do in java" substri...
Learn to use Python 打印() function to 将 Python 程序或 Python 脚本的打印输出重定向到文件. 1. 使用 文件 参数打印到文件 打印() 函数接...
Python 输入() 函数允许用户在程序中输入。 输入()函数从输入中读取一行,转换为细绳并将其作为方法返回值返回。