Home > Backend Development > Python Tutorial > How to use input python

How to use input python

藏色散人
Release: 2019-10-16 11:19:24
Original
4193 people have browsed it

How to use input python

How to use input python?

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).

Recommended: "Python Tutorial"

Note: Both input() and raw_input() functions can receive strings, but raw_input() reads them directly Input to the console (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])
Copy after login

Parameter description:

prompt: Prompt message

Example

Python2.x : input() requires input of python expression

>>>a = input("input:")
input:123                  # 输入整数
>>> type(a)
<type &#39;int&#39;>               # 整型
>>> a = input("input:")    
input:"runoob"           # 正确,字符串表达式
>>> type(a)
<type &#39;str&#39;>             # 字符串
>>> a = input("input:")
input:runoob               # 报错,不是表达式
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name &#39;runoob&#39; is not defined
<type &#39;str&#39;>
Python2.x: raw_input() 将所有输入作为字符串看待
>>>a = raw_input("input:")
input:123
>>> type(a)
<type &#39;str&#39;>              # 字符串
>>> a = raw_input("input:")
input:runoob
>>> type(a)
<type &#39;str&#39;>              # 字符串
>>>
Copy after login

The above is the detailed content of How to use input python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template