The Python function input serves the purpose of acquiring input from the user. It initiates a prompt for user input and captures a line of text. Once the data has been read, the function transforms it into a string and provides that string as its output. In cases where the end of file (EOF) is encountered, it raises an EOFError.
Python input Function Syntax
It has the following syntax:
input ([prompt])
Parameters
- prompt: This is a string message that requests input from the user.
Return
It provides the user input by transforming it into a string format.
Different Examples for Python input Function
Let’s explore a few instances of the input function to gain a clearer understanding of its capabilities.
Python input Function Example 1
In this instance, we are utilizing this function to capture user input and subsequently present it to the user.
# Python input() function example
# Calling function
val = input("Enter a value: ")
# Displaying result
print("You entered:",val)
Output:
Enter a value: 45
You entered: 45
Python input Function Example 2
The input function provides a string output. Therefore, in order to carry out arithmetic calculations, it is necessary to convert the value initially. Refer to the example provided below.
# Python input() function example
# Calling function
val = input("Enter an integer: ")
# Displaying result
val = int(val) # casting into string
sqr = (val*val) # getting square
print("Square of the value:",sqr)
Output:
Enter an integer: 12
Square of the value: 144