input: The act of entering data or any information into Python program is called input.
input sent to python program using input devices e.g keyboard,file,sensor,webcam ,touch screen etc.
In addition to the programming input can be colected from any electronic devices.
Sometimes users or developers want to run the programs with their own input data. To get input from the user In Python program the input() function is used. User can give any information to python program.
Python provides some built-in library function to handle input.
raw-input()
input()
In this tutorial we will learn how input() function is used in python .
Input() in Python
Syntax : input() in python
Given following Syntax is input() function:
# input() function
The syntax for input() looks like this:
var1= input(Message)
Explanation:
we can get input in python program using input() function.
When used in python program, this will let the the user to input some text. This text then assigned to a variable var1.
Message is the prompt displayed on console when the the input() function executes.
for e.g if we write age= input("Enter Your age:")
Then it will ask or prompt the user by displaying message
"Enter Your age:"
Then whatever the text the user enter is get assigned to the variable age.
Note:Python input() function takes all the input as a string by default. To convert it to any other data type we have to convert the input explicitly.
Given fig. show how input() statement works in python.
User feed the input to python program or script using input() function.
input() Function: the input() function in python prompt the user and accepts the input in the form of string.
input() pause the python progra till user does not provide any input from the keyboard.
Once the input is given the code or program process it, after processing the print() display or send the processed data on console i.e. output device(console).
Example 1: Python script to accept different types of values in a Program.
1. Taking input using input() , The default method in Python takes input as string.
name=input("Enter Your Name")
input() prompt the user with the message
"Enter Your Name"
Guido van Rossum
the name entered is Guido van Rossum which is get stored in name variable.
2. Accepting integer input using input()
age=int(input ("Enter Your age"))
The above function prompts "Enter Your age" .
let say we have entered 19 . 19 is considered string by input() so that we have type cast it in to the int data type.
3. Accepting Float value in python using input()
Python do not have any method to get input as float value.
we know that python read input as string but string can be converted to float using float() function.
# python script to input float value.
height=float(input(" Enter Your Height"))
4. Accepting Complex Value in Python using input()
cmplx=complex(input("Enter Complex Number"))
print("The Complex Number is ",cmplx)
output:
Enter complex Number
11+3j
The Complex Number is 11+3j
5. Accepting boolean value in Python using input()
b=bool(input("Enter value to be convert in boolean"))
print("The Value Entered in Boolean is :->",b)
output:
Enter value to be convert in boolean
1
The Value Entered in Boolean is :->True
6. Accepting tuple in Python using input()
t=input("Enter the tuple :")
t=tuple(int(a) for a in t.split(","))
print("The Tuple is-->",t)
output:
Enter the tuple :
1,2,45,67,89,23,12,50
The Tuple is--> (1,2,45,67,89,23,12,50)
Previous Topic:-->> Special Operator in Python || Next topic:-->>Output in Python