In Python, the input() function is used to take input from the user. The input() function prompts the user to provide information on which the program can work and give the result. Actually, the input() function takes the user’s input as a string. So whether we input a number or a string, it is treated as a string only.
The syntax of the Python input() function is given below:
input(prompt)
Here the parameter “prompt” specifies a string that represents a default message before the input.
Now the following example is reading variables from the user using the input() function.
Example-1
name=input("What’s your name=") age=input("Enter your age=") print(name+", you are "+age+" years old")
Output:
What’s your name=John
Enter your age=23
John, you are 23 years old
Example-2
Here we are giving another example for calculating the area of a Rectangle. Here int() function is using to convert a string value into an integer number.
length=int(input("Enter length of Rectangle=")) breadth=int(input("Enter breadth of Rectangle=")) Area=length*breadth print("The area of Rectangle is=",Area)
Output:
Enter length of Rectangle=90
Enter breadth of Rectangle=80
The area of Rectangle is= 7200
Example-3
This example is for calculating the sum of two floating-point numbers. Here float() function is using to convert a string value into a floating-point number.
a=float(input("Enter the first value=")) b=float(input("Enter the second value=")) c=a+b print("The sum of two numbers is=",c)
Output:
Enter the first value=9.25
Enter the second value=3.45
The sum of two numbers is= 12.7
Leave a comment