pythonvisual-studio-codeideraw-input

(python 3) When using raw_input(), why does the function add "\n" to the input?


I searched the first page of Google but couldn't find an answer. This won't happen for int(raw_input()) weirdly enough. But if i type:

a = [raw_input() for i in range(int(raw_input()))]
print(a)

Example input: 5 1 2 3 4 5

The output will be: ['1\n', '2\n', '3\n', '4\n', '5\n']

I know this has something to do with the IDE (I run VSCode) but I have no clue how to fix it. Thank you.


Solution

  • In Python3.x, raw_input() and input() are integrated, raw_input() is removed, only the input() function is retained, which receives any input, defaults all input to string processing, and returns the string type.

    When use Python2.7:

    enter image description here

    When use Python3.8:

    enter image description here

    In addition, if you use extensions for debugging other than the python extension in VS Code, please try to disable it to avoid affecting the results.

    My environment:

    VS Code: 1.52.1 (user setup); OS: Windows_NT x64; VS Code extension: Python; Pylance;

    Reference: What's new in Python3.