top of page

Input and Output in Python

Entering a decimal/float constant will generate an error here as int() is used

Python provides us with the input() and print() functions for input and output respectively.

The input() function

The input function is prompts the user to enter some value. When the user enters the text and presses enter, this text is returned and usually stored in a variable.

Example:

 name=input("Please enter your name")
 print(name)

In the above code the variable name is used to store the value returned by the input() function. When we run the above program in script mode the following output is generated:

 Please enter your name                   A prompt appears in front of the statement.

The name is keyed in and the screen now looks like this:

 Please enter your name Falguni
 Falguni

The first line prompts the user to enter name. When the name 'Falguni' is entered, it is stored in the variable name which accepts it as a string.

The input() function always returns a string. In order to use input() with numbers we need to typecast the return value using the  int() ,float()  or eval() functions.

Example

  marks=int(input("Enter marks : "))
  print(marks)

In the above code the int() function converts the input string to an integer which is then stored in the variable marks.

Output:

 Enter marks : 56
 56

Similarly we can use float() to convert the input string to float.

Example:

 Interest=float(Input("Enter rate of interest ")

 print("You have to pay interest %: ", Interest)

 

Output:

 Enter rate of interest 6

 You have to pay interest %: 6

Note: When entering the values using input() method, you need to ensure that the value entered is of the target type.

For example if we enter a float value when using the int() function with input(), an error is generated.:

 >>> roll=int(input("Enter roll no"))
 Enter roll no34.8
 Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    roll=int(input("Enter roll no"))
ValueError: invalid literal for int() with base 10: '34.8'

Similarly,

>>> comm=float(input("Enter Commission"))
Enter Commission 89.9%
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    comm=float(input("Enter Commission"))
ValueError: could not convert string to float: ' 89.9%' 

Sample Program: Input the Principal, Rate and time in years and calculate the Simple Interest.

The print() function

We have been using the print() statement since the beginning of this tutorial. Let us dive deeper into the variations of the print() statement. The basic syntax of the print () command is:

  print(*objects, sep=' ', end='\n')

where the arguments:

 *objects           refers to  variables, constants etc and * refers to more than one object in a single print statement

 sep                  we can put a separator like a comma, ;, : or any other character between objects or strings. The 

                         default separator is a ' '

 end                  This is the delimiter character or the character which is printed at the end. 

Printing multiple objects

To print more than one object (variable, constant or string) we can simply use commas in between.

Examples:

 >>>print("Hello", "Welcome")

 Hello Welcome                                                               #Automatically inserts space pace in between the strings as the

                                                                                        default sep=' '     

>>>print(89/2)                                                               # prints the result of a mathematical calculation

44.5

 >>>print("The sum of ", 45, " and ", 65, "is :", 45+65)

 The sum of 45 and 65 is :110                                        #printing different types of objects

Using the separator parameter

 

The sep parameter can be used to separate text or data using any character like a comma, semicolon etc.

Examples:

>>> print("Name","Place","Animal","Thing", sep=',')
 Name,Place,Animal,Thing 
                                          # A comma is used as a separator 

>>> print("Result is", 45/2, sep=';')                               # A semicolon is used as a separator 

Result is;22.5

 

>>> print("name","gmail.com",sep='@')                       # An @ is used as a separator 

 name@gmail.com

 

Using the end parameter

 

The default value of the end argument of the print() command is a newline('\n'). So a newline is appended to the output if not specified otherwise. But we can specify any character to be printed at the end.

 

Examples:

 >>> print("Name","Place","Animal","Thing", end='@@')
 Name Place Animal Thing@@ 
                                   # The characters in the parameter end are printed after the text     

>>>print("It will be", 8900/400, end="$")               # The character $ in the parameter end is printed after the output text

 It will be 22.25$

 

>>>print("It will be", 7800/2,sep=":", end="Rs")           # The letters Rs in the parameter end is printed after the output

 It will be:3900.0Rs

Using end parameter in script mode

In the script mode when we write consecutive print statements the output is displayed on separate lines. But if we wish to print the output of both statements on same line we can assign a ' ' to the parameter end.

 

 print(67+78,end=' ')
 print(45+90)

Output:


145 135

Sample Program: Write a program to input the radius of a circle and calculate the area and circumference.

Here the user has entered a character % which will generate an error 

Input Function
Input and Target type
Print function
Printing multiple objects
Separator parameter
End Parameter
End parameter in Script mode
Sample Program
bottom of page