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

黄舟
Release: 2017-10-07 11:40:33
Original
1701 people have browsed it


Experiment

a = input('请输入:')
print a
Copy after login

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>
Copy after login

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

请输入:1010
Copy after login

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

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

The reason

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

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

and raw_input can accept string input:

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

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!

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