Python raw_input() Function

Python 内置函数Python Built-in Functions

Python raw_input() is used to obtain input from the console.

raw_input() treats all input as a string and returns a string type.

Note: Both input() and raw_input() can receive strings, but raw_input() directly reads console input (it can accept any type of input). For input(), it expects to read a legal Python expression; that is, when you enter a string, you must enclose it in quotes, otherwise it will raise a SyntaxError.

Unless you have a special need for input(), in general we recommend using raw_input() to interact with the user.

Note: In Python 3, input() receives the str type by default.

Function Syntax

raw_input([prompt])

Parameter description:

  • prompt: Optional, string; can be used as a prompt.

Examples

raw_input() treats all input as a string

>>>a = raw_input("input:") input:123 >>> type(a) <type 'str'> # string >>> a = raw_input("input:") input:example >>> 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:"example" # correct, string expression >>> type(a) <type 'str'> # string >>> a = input("input:") input:example # error, not an expression Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'example' is not defined <type 'str'>

Python 内置函数Python Built-in Functions

Other Extensions