input() Builtin Function
Python input()
Python input() builtin function is used to read input from user via standard input.
In this tutorial, you will learn the syntax of input() function, and then its usage with the help of example programs.
Syntax
The syntax of input()
function is
input()
input(prompt)
where
Parameter | Description |
---|---|
| A string, that is printed to the standard output, before input() function starts reading the value input by user. prompt is helpful in letting the users know what kind of input the application is expecting, something like a clue. Prompt is optional though. |
When you call this input() function, Python Interpreter waits for user input from the standard input. When user enters a value in the console input, using keyboard, each character is echoed to the output. This mechanism acts a feedback to user letting know about what is being input. After you enter the value and hit Return or Enter key, the string that is keyed in by you until this last enter key, is returned by the input() function.
Examples
1. Read input from user
In the following program, we read user's name from user via standard input using input() function.
Python Program
name = input()
print('Hello', name)
Output
2. Display prompt to user while reading input
We can also pass a prompt string as argument to input function.
In the following program, we prompt 'Enter you name :'
to the standard output, before user enters an input.
Python Program
name = input('Enter your name :')
print('Hello', name)
Output
3. Read integer using input()
By default, input() returns only a string value. If we want to read an integer or float, we may have to convert the value returned by input() function to desired datatype.
For example, in the following program, we read an integer from user. As already said, this is a two step process. First we read input from user, and then convert it to an integer using int() builtin function.
Python Program
age = int(input('Enter your age : '))
print('You age is', age)
Output
Summary
In this tutorial of Python Examples, we learned the syntax of input() builtin function, and how to read a value entered by user via standard input, with examples.