Python Output and Input

Table of contents

You may have already seen the print() function in previous tutorials so I believe you already have some idea what this function does. We will go through the print() function in more detail here.

The built-in function print() is used for standard output in python. It is also possible to store the output in a file but we will study that later. The print() function can be used to display the output on the console.

For example:

print('This is a print example.')
a = 100
print('The value of a is: ', a)


This is a print example.
The value of a is 100

In the example above, the first print() function is straightforward as we just passed a string to it and it printed the string. However, in the second print() function we passed a string and a variable separated by commas and both of them are printed. That is, you can pass any number of objects to print into the same print() function.

Apart from objects to print, the print() function also allows other parameters. The parameters supported by this function are:

  • objects: Objects are the values to be printed. This parameter is mandatory, that is you need to pass at least one object to print. If you are passing multiple objects, they need to be separated by a comma (,).
  • sep: sep parameter is optional and can be passed when you have multiple objects to print. The default value of sep parameter is space (‘ ‘) character.
  • end: The value in the end parameter is printed after all the objects in the print() function are printed. The default value to the end parameter is a newline (\n) character. This is also optional.
  • file: This parameter can be used to specify where to print the objects. The default value of the file parameter is sys.stdout which means console or screen. We can use this parameter to print the objects to a file but we will not cover that in this post.
  • flush: The optional parameter flush can be used to forcibly flush the stream. The default value is False and it will flush the output if set to true.


print(objects, sep = ' ', end = '\n', file = sys.stdout, flush = False)

For example:

var1 = 100
var2 = 200
print(var1, var2, sep = '



In the above example, var1 and var2 are two objects. Since sep = ‘$’, those two objects are printed with separator ‘$’ in between. Also, ‘&’ is printed at the end as end = ‘&’.

Python Output Formatting Using format() Method

The string class in python has a method str.format() which allows you to format the output. You can apply this method to any string object. This method allows you to include one or more placeholders or replacement fields represented by curly brackets { } into a string. The value(s) you want to replace those curly brackets { } are passed into the method format().

For example:

print('I have { } strawberries.'.format(5))
print('I have { } strawberries and { } apples.'.format(5, 6))
print('I have {1} strawberries and {0} apples.'.format(5, 6))


I have 5 strawberries.
I have 5 strawberries and 6 apples.
I have 6 strawberries and 5 apples.

In the first print() function, the { } is replaced by 5 that is passed into the format() method. Similarly, 5 and 6 replace both curly brackets in the second print() function in an orderly manner. However, in the third print() function, you can see that the order is specified within the curly brackets. That means, the second parameter 6 passed into the format() method replaces the first {1} and the first parameter 5 replaces the second {0}. Remember that, the indexing starts from 0 in python.

The print() function returns None which is python equivalent of Null or empty.

Python Input Using input() Function

In python, built-in function input() is used to take input from the users. The input() function converts the user input into a string.

For example:

a = input()

If you run this, you will be asked to enter an input. The input will be assigned to the variable ‘a’. Remember that if you want to take numerical input and perform mathematical operations on it, you will have to convert the data type to integer or float from a string as the input() function converts the input to a string.

Python input() Function Parameters

The only optional parameter allowed by input() function is:

  • prompt: If you wish to display a prompt while asking for input, you can pass a string to the input() function. The prompt message will be displayed on the console without a newline. This parameter is optional.

For example:

age = input('Please enter your age: ')

What does the input() function return in Python?

The input() function converts the user input to a string and returns the converted string.