Home >Backend Development >Python Tutorial >How to use python input() function
How to use the python input() function?
The input() function in Python3.x accepts a standard input data and returns it as string type.
In Python2.x, input() is equivalent to eval(raw_input(prompt)), which is used to obtain console input.
raw_input() treats all input as strings and returns the string type. And input() has its own characteristics when dealing with pure numeric input. It returns the type of the entered number (int, float).
Note:
Both the input() and raw_input() functions can receive strings, but raw_input() directly reads the input from the console (any It can accept any type of input). As for input(), it hopes to be able to read a legal python expression, that is, you must use quotes to enclose it when you enter a string, otherwise it will raise a SyntaxError.
Unless there is a special need for input(), we generally recommend using raw_input() to interact with users.
Note: input() in python3 receives the str type by default.
Function syntax
input([prompt])
Parameter description:
prompt: Prompt message
Example
Python2.x : input() requires input of python expression
>>>a = input("input:") input:123 # 输入整数 >>> type(a) <type 'int'> # 整型 >>> a = input("input:") input:"runoob" # 正确,字符串表达式 >>> type(a) <type 'str'> # 字符串 >>> a = input("input:") input:runoob # 报错,不是表达式 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'runoob' is not defined <type 'str'>
Python2.x: raw_input() treats all inputs as strings
>>>a = raw_input("input:") input:123 >>> type(a) <type 'str'> # 字符串 >>> a = raw_input("input:") input:runoob >>> type(a) <type 'str'> # 字符串 >>>
Related recommendations: "Python Tutorial》
The above is the detailed content of How to use python input() function. For more information, please follow other related articles on the PHP Chinese website!