python input function

Python provides numerous built-in functions that are readily available to us at the Python prompt. Some of the functions like input() and print() are widely used for standard input and output operations respectively. Let us see the output section first.

input() Parameters

The input() method takes a single optional argument:

  • prompt (Optional) – a string that is written to standard output (usually screen) without trailing newline

Return value from input()

The input() method reads a line from input (usually user), converts the line into a string by removing the trailing newline, and returns it.

 

Example 1: How input() works in Python?

  1. # get input from user
  2. inputString = input()
  3. print('The inputted string is:', inputString)

When you run the program, the output will be:

Python is interesting. The inputted string is: Python is interesting

Example 2: Get input from user with a prompt

  1. # get input from user
  2. inputString = input('Enter a string:')
  3. print('The inputted string is:', inputString)

When you run the program, the output will be:

Enter a string: Python is interesting. The inputted string is: Python is interesting