Python input() Function



The Python input() function provides a way to accept input from the user. It holds the program's execution and waits for the user to provide the required input. You can also use the prompt parameter to display a custom message to the user before the input.

The input() function is one of the commonly used built-in functions, and you can use this method directly without importing any module. It accepts any type of data but returns the given data in the string format.

There are some functions that are used with input() (such as int(), float(), etc.) to convert the given input in a specific type of format.

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.

input() Function Examples

Practice the following examples to understand the use of input() function in Python:

Example: Use of input() Function

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: Integer and Float Input Using input() Function

To take input of data types 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: Continuously Prompt for User Input

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