Python版本:Python 2.7.13rc1
ex1.py :
name = input("What's your name? ")
print("Nice to meet you " + name + "!")
name2 = raw_input("What's your name? ")
print("Nice to meet you " + name2 + "!")
input():
input会解释用户的输入。如果用户输入一个整数值,那么input()会返回一个整数值。在使用input()的时候,如果需要输入string, 那么需要在输入的时候加上引号。否则,Python会把这个输入当做变量来处理。
PS C:\pyex> python .\ex1.py
What's your name? John
Traceback (most recent call last):
File ".\ex1.py", line 1, in <module>
name = input("What's your name? ")
File "<string>", line 1, in <module>
NameError: name 'John' is not defined
Note:尽量注意避免使用input(),因为会有安全问题。
raw_input():
raw_input不会解释输入,直接返回用户输入的内容,并不会有任何改变.。raw_input返回的是string类型,如果需要它返回整型,需要进行转换 x = int(raw_input()) 。
对比input()和raw_input()的输出结果
PS C:\pyex> python .\ex1.py
What's your name? "John\n"
Nice to meet you John
!
What's your name? "John\n"
Nice to meet you "John\n"!