input() 函数用于获取用户输入,并以字符串格式返回输出。用户可以输入字符串或数字格式的数据。
input([prompt]) #Where prompt is a string value
接受单个参数。用户输入的任何内容都作为输入参数。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| prompt | 一个写入标准输出(通常是屏幕)的字符串,没有尾随换行符。 | 可选 |
input() 函数实际上会评估输入字符串并尝试将其作为 Python 代码运行,输出以字符串形式返回。
| 输入 | 返回值 |
|---|---|
| 字符串 | 通过删除尾随换行符生成的字符串。 |
| 如果遇到 EOF | 它会引发 EOFError 异常。 |
# get input from user
inputString = input()
print('The inputted string is:', inputString)
输出
Python is interesting. The inputted string is: Python is interesting
# get input from user
inputString = input('Enter a string:')
print('The inputted string is:', inputString)
输出
Enter a string: Python is interesting. The inputted string is: Python is interesting
# take three values from user
name = input("Enter Employee Name: ")
salary = input("Enter salary: ")
company = input("Enter Company name: ")
# Display all values on screen
print("\n")
print("Printing Employee Details")
print("Name", "Salary", "Company")
print(name, salary, company)
输出
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google
# convert inout into int
first_number = int(input("Enter first number ")) sec second number "))
print("\n")
print("First Number:", first_number)
print("Second Number:", second_number)
sum1 = first_number + second_number
print("Addition of two number is: ", sum1)
输出
Enter first number 28 Enter second number 12 First Number: 28 Second Number: 12 Addition of two number is: 40