Python2.x中的raw_input()和Python3.x中的input()函数有很大的不同,Python2.x中的raw_input()函数只能接受字符串类型的输入,而Python3.x中的input()函数可以接受任何类型的输入;Python2.x中的raw_input()函数会将输入的内容当做字符串来处理,而Python3.x中的input()函数会将输入的内容当做表达式来处理;Python2.x中的raw_input()函数不支持对输入内容进行格式化,而Python3.x中的input()函数支持对输入内容进行格式化。
使用方法
Python2.x中使用raw_input()函数:
#定义一个变量 name = raw_input('请输入你的名字:') #输出变量 print(name)
Python3.x中使用input()函数:
#定义一个变量 name = input('请输入你的名字:') #输出变量 print(name)
Python2.x中使用raw_input()函数进行格式化输入:
#定义一个变量 name = raw_input('请输入你的名字:') #格式化输出变量 print('你好,%s' % name)
Python3.x中使用input()函数进行格式化输入:
#定义一个变量 name = input('请输入你的名字:') #格式化输出变量 print('你好,{0}'.format(name))
:
- Python2.x中的raw_input()函数只能接受字符串类型的输入,而Python3.x中的input()函数可以接受任何类型的输入;
- Python2.x中的raw_input()函数会将输入的内容当做字符串来处理,而Python3.x中的input()函数会将输入的内容当做表达式来处理;
- Python2.x中的raw_input()函数不支持对输入内容进行格式化,而Python3.x中的input()函数支持对输入内容进行格式化。