Search code examples
pythonstringinputnewlineprompt

Why is there a [ ] in input([prompt])?


https://docs.python.org/3/library/functions.html#input

See the above link. Now, the below questions may be silly, but wanted to make myself clear.

First Question: Why is there a [] in input([prompt])? Does it signify anything?

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

Second Question: Why does it need to convert the input to a string? Isn't the input from the sys.stdin already a string?

Third Question: What does stripping a trailing newline means in this case?


Solution

  • The [] in the python docs indicates optional arguments - not, confusingly, the need for a list as an argument.

    The input from a modern UTF-8 terminal may well be bytes, so conversion to a string is done to avoid confusion.

    stripping a trailing newline means that it takes the newline off the end, so you don't end up with it in your string.