Input, Output and Import
Functions
Python input() Function
Python input() function is used to take user input. By default, it
returns the user input in form of a string.
Returns: Return a string value as input by the user.
By default input() function helps in taking user input as string. If
any user wants to take input as int or float, we just need to
typecast it.
Python input() Function
Example
# Taking input as string
color = input("What color is rose?: ")
print(color)
# Taking input as int
# Typecasting to int
n = int(input("How many roses?: "))
print(n)
# Taking input as float
# Typecasting to float
price = float(input("Price of each rose?: "))
Example 1: Taking the Name and Age of the user as input and
printing it
Example 2: Taking two integers from
users and adding them.
In this example, we will be looking at how to take integer input from
users. To take integer input we will be using int() along with Python
input()
# Taking number 1 from user as int
num1 = int(input("Please Enter First Number: "))
# Taking number 2 from user as int
num2 = int(input("Please Enter Second Number: "))
# adding num1 and num2 and storing them in
# variable addition
addition = num1 + num2
# printing
print("The sum of the two given numbers is {} ".format(addition))
Output:
Similarly, we can use float() to take two float numbers.