Python input() 函数用于获取用户的输入。它提示用户输入并读取一行。读取数据后,它将其转换为字符串并返回。如果读取 EOF,它会抛出错误 EOFError。
签名
input ([prompt])
参数
prompt: 这是一个提示用户输入的字符串消息。
返回
它在转换为字符串后返回用户输入。
让我们看一些 input() 函数的例子来理解它的函数。
Python input() 函数示例 1
在这里,我们使用这个函数获取用户输入并显示给用户。
# Python input() function example
# Calling function
val = input("Enter a value:")
# Displaying result
print("You entered:",val)
输出:
Enter a value:45 You entered:45
Python input() 函数示例2
input() 方法返回字符串值。所以,如果我们要执行算术运算,我们需要先对值进行转换。请参阅下面的示例。
# Python input() function example
# Calling function
val = input("Enter an integer:")
# Displaying result
val = int(val) # casting into string
sqr = (val*val) # getting square
print("Square of the value:",sqr)
输出:
Enter an integer:12 Square of the value:144
相关用法
- Python input()用法及代码示例
- Python int()用法及代码示例
- Python id()用法及代码示例
- Python string isidentifier()用法及代码示例
- Python calendar isleap()用法及代码示例
- Python math isclose()用法及代码示例
- Python string isupper()用法及代码示例
- Python string isalnum()用法及代码示例
- Python numpy matrix identity()用法及代码示例
- Python itertools.groupby()用法及代码示例
- Python isdisjoint()用法及代码示例
- Python string istitle()用法及代码示例
- Python itertools.repeat()用法及代码示例
- Python math isnan()用法及代码示例
- Python string isalpha()用法及代码示例
- Python calendar itermonthdays2()用法及代码示例
- Python dict items()用法及代码示例
- Python Tkinter iconphoto()用法及代码示例
- Python calendar itermonthdays()用法及代码示例
- Python string isdigit()用法及代码示例
注:本文由纯净天空筛选整理自 Python input() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。