Python input() Function | Explained With Examples

In programming languages, user interaction with the code script is very important while performing simple to advance tasks. A user interacts with Python’s program code when they input data or output it. In Python, an inbuilt function named “input()” is used to input/take the data from the user.

This write-up will provide a comprehensive guide on the Python input() function with multiple examples. The following contents are presented in this Python blog:

So let’s begin the Python guide!

What is the Python input() Function?

The “input()” function in Python is an inbuilt function used to input data. The syntax of the “input()” function is shown below:

input([prompt])
How to install Kali Linux in Window...

In the above syntax, the “prompt” is an optional parameter. It is used only to show the user any message while inputting data. When the “input()” function is used in Python script, it asks the user to enter any data before proceeding further in the program. The input() function accepts the input value as a string.

Example 1: Using input() Function to Get Value From the User

In the example code shown below, the “input()” function takes the value from the user. The user-stored value is displayed on the output screen using the “print()” function.

Code:

First_Name = input("Enter a Name: ") 
# Displaying result 
print("Please Verify Your Name:",First_Name)

In the above code:

  • The “input()” function takes the value from the user. The statement shown to the user on the screen is initialized inside the parentheses of the “input()” function.
  • The “print()” function displays the user input value.

Output:

The above output verifies the working of the “input()” function.

Example 2: Using input() Function to Get Integer Value From the User

In the example code shown below, the “input()” function takes the integer value from the user. The user-stored value is converted into an integer using the “int()” function for further mathematical operation in the program.

Code:

First_value = input("Enter an integer value: ")

# Displaying result 
convert_value = int(First_value) 
sqr_value = (convert_value*convert_value)
print("Square of the input value:",sqr_value)

In the above code:

  • The “input()” function accepts the integer value from the user. The integer value will be in the string format.
  • To use integer value for further mathematical operations, the user-entered string value will be converted into an integer using the “int()” function.
  • The mathematical operation “square” is performed on the converted value, and the final result is stored in a variable named “sqr_value”.

Output:

The above output shows that the “input()” function accepts the user-entered value.

That’s all from this python tutorial!

Conclusion

In Python, the inbuilt function named “input()” is used to take the user input in the form of a string. The user input string value can be converted into an integer or any other data type using the built function like “int()”, “float()”, “str()”, etc. The “input()” function has a “prompt” parameter that displays a message in string data type. The input() function of Python was explained in this guide with numerous examples.