Python input() Function



The Python input() function is a built-in function which provides a way to accept input from user. It pause the program and wait for the user to enter required data. Whatever the user types is then returned as a string.

Syntax

Following is the syntax of the Python input() function −

input(message)

Parameters

The Python input() function accepts a single parameter −

  • message − It represents a String which provides information related to user input.

Return Value

The Python input() function returns a String containing the user input.

Examples

In this section, we will see some examples of input() function −

Example 1

The following example shows how to use the Python input() function. Here we are defining a string and applying the input() function to convert it into a String with input characters. The result will be the same as original string as it already has ASCII characters.

orgName = input("Enter your organisation name: ")
print(f"Welcome to, {orgName}")  

On running the above program, it will ask to enter organisation name. When we press enter after entering the name, it will print the following result −

Enter your organisation name: Tutorialspoint
Welcome to, Tutorialspoint

Example 2

To take input of datatypes in Python, we need to use the input() function by wrapping it into the corresponding type conversion function. In this example, we are taking input of integer and float type.

numbOne = float(input("Enter first num: "))
numbTwo = int(input("Enter second num: "))
addition = numbOne + numbTwo
print(f"The sum of {numbOne} and {numbTwo} is {addition}")  

When we run the above program, it will prompt users to enter values −

Enter first num: 25.6
Enter second num: 52
The sum of 25.6 and 52 is 77.6

Example 3

In the following example, the program will continuously ask the user for input until they type "exit".

while True:
   userInp = input("Enter 'exit' to quit: ")
   if userInp.lower() == "exit":
      break
   else:
      print("Entered value:", userInp)  

When we run the above program, it will prompt users to enter values −

Enter 'exit' to quit: 5
Entered value: 5
Enter 'exit' to quit: TP
Entered value: TP
Enter 'exit' to quit: exit
python_built_in_functions.htm
Advertisements