Python Output | Printing Output in Python

If you are trying to understand Python output printing methods, then by now you must have been aware of Python input taking methods. In this article, let’s have a look at the different ways to output variables and statements in Python 3. 

Printing the Output in Python

The simplest way to print Python output is by using the print() inbuilt function. The syntax for print() function is

Syntax: print (value(s), sep= '  ', end = '\n', file = file, flush = flush)

Here,

  • value(s): You can insert any value or as many values as you want. Any value irrespective of its type will be converted to a string before printing.
  • sep = ‘ ‘ : This is an optional parameter in the syntax. In the space between ‘ ‘, you need to specify how to separate the values you have entered if there is more than one. Space is the default separator.
  • end = ‘end’ : This is also optional. Using end, you need to specify what to print at the end. The default end is ‘\n’.
  • file : (Optional) File is the place where this print statement has to be printed. By default it is sys.stdout
  • flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). The default value of flush is False.

Python Output Printing Examples

1) Printing a String or Text:

You can print a line or a string either inside double quotes or single quote as shown below. 

print ('Hello World!')   
print ("I am Learning Python on FACE Prep")
Output:
Hello World!
I am Learning Python on FACE Prep

2) Printing a Value/Integer:

Consider a value 5 stored in the variable named num. To print that value, we need to just mention the variable name as shown below.

num = 5
print (num)
Output: 5

3) Printing a Value along with a String:

Consider, you need to print a statement ‘Cats have 4 legs’. Where the value 4 is stored in a variable num. Then your code should look something as shown below,

num = 4
print ("Cats have", num, "legs")
Output: Cats have 4 legs

If you need to print one more statements in the next line, then just write one more print statement as shown below,

num = 4
print("Cats have", num, "legs")
print(“And I love Cats”)
Output:
Cats have 4 legs
And I love Cats

Note: Newline is inserted at the end of each print statement, by default.

4) Printing a String using sep

Consider the following code,

num = 4
print ("Cats have", num, "legs")
print ('Hello','all')
Output:
Cats have 4 legs
Hello all

Now, consider the below code with ‘sep’ parameter

num = 4
print ("Cats have", num, "legs")
print ('Hello','all', sep = '')
Output:
Cats have 4 legs
Helloall

4) Printing a String using end

End is used to append space instead of a new line. Consider the below code,

num = 4
print ("Cats have",num,"legs")
print ('Hello','all')
print ("Good day")
Output:
Cats have 4 legs
Hello all
Good day

Now, consider the below code with ‘end’ parameter

num = 4
print ("Cats have",num,"legs")
print ('Hello','all',end = ' ')
print ("Good day")
Output:
Cats have 4 legs
Hello all Good day

You can also give any special characters inside ‘end’ parameter as shown below,

num = 4
print ("Cats have", num, "legs")
print ('Hello', 'all', end = ' :) ')
print ("Good day")
Output:
Cats have 4 legs
Hello all :) Good day

Consider another example, in which the parameter ‘sep’ removed the space and appended the symbol ‘-’ between the numbers.

print("21","05","1997", sep = '-')
print("21","05", sep = "-", end = "-1997")
Output:
21-05-1997
21-05-1997