Home >Backend Development >Python Tutorial >Python: Detailed explanation of input() and raw_input()

Python: Detailed explanation of input() and raw_input()

黄舟
黄舟Original
2017-10-07 11:40:331714browse


Experiment

a = input('请输入:')
print a

If you enter a string, an error will be reported immediately:

请输入:str  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>

But if you enter an integer, no error will be reported:

请输入:1010

If input is changed to raw_input, the keyboard input string can be recorded normally:

a = raw_input(&#39;请输入:&#39;)print a
请输入:str
str

The reason

The reason is that, input can only accept integer input:

a = input(&#39;请输入:&#39;)print type(a)
请输入:10<type &#39;int&#39;>

and raw_input can accept string input:

a = raw_input(&#39;请输入:&#39;)print type(a)
请输入:str
<type &#39;str&#39;>

The above is the detailed content of Python: Detailed explanation of input() and raw_input(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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