input()
函数有助于接受用户的输入,并以字符串格式返回输出。用户可以输入字符串或数字格式。
**input([prompt])** #Where prompt is a string value
接受单个参数。无论用户键入什么,它都将作为输入参数。
参数 | 描述 | 必需/可选 |
---|---|---|
提示 | 写入标准输出(通常是屏幕)的字符串,不带尾随换行符 | 可选择的 |
input()
函数实际上是对输入字符串进行求值,并尝试将其作为 Python 代码运行,输出作为字符串返回。
| 投入 | 返回值 | | 线 | 通过移除尾部换行符来创建字符串 | | 如果电渗流 | 它会引发 EOFError 异常。 |
input()
方法的示例 # 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("
")
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("
")
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