The input()
function in Python is a built-in function that reads a line from the input (usually from the user) and returns it as a string. It allows the program to interact with the user by prompting the user to enter some value.
Parameter Values
Parameter | Description |
---|---|
prompt | A string that is printed on the screen while waiting for the user to input data. It informs the user what kind of input is expected. |
Return Values
The input()
function in Python always returns a str
(string) type.
How to Use input()
in Python
Example 1:
Read a integer number from the user and print it back
num = int(input
('Enter a number: '))
print('You entered:', num)
Example 2:
Ask for user's name and print a greeting message
name = input
('What is your name? ')
print('Hello,', name, 'Nice to meet you!')
Example 3:
Take a list of numbers as input and calculate the sum
numbers = [int(x) for x in input
('Enter numbers separated by space: ').split()]
sum_of_numbers = sum(numbers)
print('Sum of numbers:', sum_of_numbers)