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

前端 未结 2 541
误落风尘
误落风尘 2020-12-07 02:40

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

2条回答
  •  情歌与酒
    2020-12-07 03:11

    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()).

提交回复
热议问题