Python raw_input() Function
Python raw_input() is used to obtain input from the console.
raw_input() treats all input as strings and returns a string type.
Note: both input() and raw_input() can accept strings, but raw_input() reads console input directly (it can accept any type of input). input(), however, 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
raw_input([prompt])
Parameter Description:
- prompt: optional, a string that can be used as a prompt message.
Example
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
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 built-in function