Home  >  Article  >  Backend Development  >  Comparison between input and raw_input in Python

Comparison between input and raw_input in Python

黄舟
黄舟Original
2017-08-20 10:15:401524browse

This article mainly introduces the relevant information about the comparison between input and raw_input in Python. Through this article, I hope it can help everyone. For the usage and differences between them, friends who need it can refer to it

Comparison between input and raw_input in Python

Both input and raw_input can receive input, the difference is as follows:


#input假设用户输入的是合法的Python表达式
>>> name = input("what is your name?")
what is your name?ZJ
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<string>", line 1, in <module>
NameError: name &#39;ZJ&#39; is not defined

#于是,必须这么使用,输入Python格式的字符串"ZJ"
>>> name = input("what is your name?")
what is your name? "ZJ"
>>> print name
ZJ
>>> 

#raw_input会把所有输入当作原始数据(raw data),然后将其放入字符串中
>>> name = raw_input("what is your name?")
what is your name?ZJ
>>> print name
ZJ
>>>

Therefore, in general, raw_input should be used as much as possible.

The above is the detailed content of Comparison between input and raw_input in Python. 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