Python input() Function
In Python 3.x, the input() function reads a line from standard input and returns it as a string.
In Python 2.x, input() is equivalent to eval(raw_input(prompt)) and is used to obtain input from the console.
raw_input() treats all input as a string and returns a string. input(), however, behaves differently with purely numeric input: it returns the type of the number entered (int, float).
Note:Both input() and raw_input() can accept strings, but raw_input() reads console input directly (it can accept any type of input). input(), on the other hand, expects a valid Python expression, so when you enter a string you must enclose it in quotes, otherwise it raises a SyntaxError.
Unless you have a specific need for input(), it is generally recommended to use raw_input() for interacting with users.
Note:In Python 3, input() receives input as type str by default.
Function syntax
input([prompt])
Parameter Description:
- prompt: the prompt message
Example
Python 2.x: input() requires a Python expression as input
a
=
input
(
"
input:
"
)
input
:
123
Enter an integer
type
(
a
)
<
type
'
int
'
Integer
a
=
input
(
"
input:
"
)
input
:
"
runoob
"
Correct, a string expression
type
(
a
)
<
type
'
str
'
String
a
=
input
(
"
input:
"
)
input
:
runoob
Error, not an expression
Traceback
(
most
recent
call
last
)
:
File
"
"
,
line
1
,
in
<
module
File
"
"
,
line
1
,
in
<
module
NameError
:
name
'
runoob
'
is
not
defined
<
type
'
str
'
Python 2.x: raw_input() treats all input as strings
a
=
raw_input
(
"
input:
"
)
input
:
123
type
(
a
)
<
type
'
str
'
String
a
=
raw_input
(
"
input:
"
)
input
:
runoob
type
(
a
)
<
type
'
str
'
String
More content
Differences between raw_input() and input() in Python 2.x and Python 3.x
Python built-in function