top of page

Python Input and Output

The function input( ) allows us to take input at run time interactively from user and store this value in a variable or object.

Input in Python (Reading String / Value)

Most of the time in our program, we want to input data / value at run time, i.e. we want to make our program user interactive.

In Python 3.x, we can use built-in function input( ). The function input( ) allows us to take input at run time interactively from user and store this value in a variable or object.

Syntax :

Variable or Object = input(“Prompt Message to be displayed”)

Example

Name = input(“Enter your name :: “)

CityName = input(“Enter your city name :: “)

When you type this above code in Python Shell, you will get a prompt message and waiting cursor for your response. Whatever value entered by you, that will be assigned to the variable written before assignment operator like Name and CityName.

Note : input( ) function always returns a value of String type.

Please look carefully on the code given in sample and its output.

#Que : Write a program in Python to add two numbers.

num1 = input (“Enter Number 1 : “)

num2 = input (“Enter Number 2 : “)

sum = num1 + num2

print(“Sum of “, num1 , “and”, num2, “is”, sum)

#Output : : ?????

Please try this program before watching video further…..

Okay! Lets see,


Output :

Sum of 20 and 25 is 2025

Why? 2025 

Why not 45.


Let’s see some more interesting example –

x = input("Enter No ")

Enter No : 25

>>> type(x)

type <str>


Ite means that input method always return a string value, either you entered number i.e. digits.


So when you input two numbers in num1 and num2, both contain string values '20' and '25' respectively.

and 

sum = num1 + num2

is 

sum = '20' + '25'

so it concatenate not add, and it gives '2025'



How to resolve these problems?


Type Casting/ Data Conversion Method in Python –

Python provide us a rich set of type casting / data conversion method, which convert any string to specified numeric, and other data types.


int ( ) – convert any number convertible type of value to integer, i.e. string, which containing digit, to integer, float to integer and bool value to integer. Be careful, while converting string to integer, it must contain only digits, otherwise raise a ValueError message.

val = input("enter no")

x = int(val)

or 

x = int(input("enter no"))


float ( ) – convert any type of value to fractional number, i.e. string to float, integer to float and bool value to float.

v = float('2569.39')    # convert '2569.39' into 2569.39


complex( ) - convert any type of value to complex number, i .e. string to complex, integer to complex, float to complex and bool value to complex.


bool( ) - convert any type of value to Boolean value, i.e. string to Boolean, integer to Boolean, Float to Boolean.


str( ) – convert any type of values to String value, i.e. number to string, Boolean value to string value, complex to Boolean, etc.


Using of Type Conversion Method with input( ) statement


num1 = int( input (“Enter Number 1 : “))

num2 = int (input (“Enter Number 2 : “) )

sum = num1 + num2

print(“Sum of “, num1 , “and”, num2, “is”, sum)


Output:

Enter Number 1 : 20

Enter Number 2 : 25

Sum of 20 and 25 is 45.

;)

Python Input Output
bottom of page