-2

`

There's an example in 2. Built-in Functions

    >>> s = input('--> ')  
    --> Monty Python's Flying Circus
    >>> s  
    "Monty Python's Flying Circus"

Though I understand how to use input, cannot understand it's princile intuitively.
How does input magically stop to wait me and know I am texting in?

Wizard
  • 2,503
  • When you posted this question, how did you ‘‘magically’’ know to stop typing and wait for a response?   Seriously, if you understand how to use input, what do you not understand? – G-Man Says 'Reinstate Monica' Apr 07 '18 at 02:37
  • The same way Python knows to print the value of s when you typed in s and pressed Enter. – muru Apr 07 '18 at 02:41

1 Answers1

0

input() is waiting for you to enter something because that's part of its documented function:

input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

So looking at the example given:

s = input('--> ')

You're calling the input() function, which does the following:

  1. Prints the optional prompt argument (-->) to standard output (in this case is your console);
  2. Reads a line from standard input (ending when you supply a newline by pressing Enter);
  3. Either:

    a. Removes the trailing endline from the input and converts the remainder to a string (if an end of file condition has not been detected)

    b. Raises an EOFError error (if an end of file condition has been detected)

  4. Returns the string.

The returned string is then stored in a variable called s. In a non-interactive program, if you wanted to display the value stored in s, you would have to use:

print(s)

Instead of simply s.

ErikF
  • 4,042