Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

text - Differences between input commands in Python 2.x and 3.x

Ok, so i use a lot of input commands, and I understood that in Python2 I can do:

text = raw_input ('Text here')

But now that i use Python 3 I was wondering what's the difference between:

text = input('Text here')

and:

text = eval(input('Text here'))

when do I have to use the one or the other?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Python 3.x, raw_input became input and Python 2.x's input was removed. So, by doing this in 3.x:

text = input('Text here')

you are basically doing this in 2.x:

text = raw_input('Text here')

Doing this in 3.x:

text = eval(input('Text here'))

is the same as doing this in 2.x:

text = input('Text here')

Here is a quick summary from the Python Docs:

PEP 3111: raw_input() was renamed to input(). That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(input()).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...