Input Output in Python

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.

Output in Python

Python uses the print() function to output data to standard output device — the screen. The function print() evaluates the
expression before displaying it on the screen. The print() outputs a complete line and then moves to the next line for subsequent output.

The syntax for print() is:

print(value [, …, sep = ‘ ‘, end = ‘\n’])

  • sep: The optional parameter sep is a separator between the output values. We can use a character, integer or a string as a separator. The default separator is space.
  • end: This is also optional and it allows us to specify any string to be appended after the last value. The default is a new line.

>>> print(“Hello”)

Hello
>>> print(10*2.5)

25.0
>>> print(“I” + “love” + “my” + “country”)
Ilovemycountry

Note : + operator concatenates the string.
>>>print(“I’m”, 16, “years old”)

I’m 16 years old

Note : , (comma) separate the value and put space in between different values.

>>> x = 20

>>> y = 30

>>> print(“X “, x, ‘\nY ‘, y)

X 20

Y 30


Class 11 Computer Science


Informatics Practices



Leave a Comment

You cannot copy content of this page

Scroll to Top