How To Use Python Print Function Tutorial with Examples – POFTUT

How To Use Python Print Function Tutorial with Examples

Python is a very expressive language that provides a lot of different output types and extensions. print() is one of the most used functions to print output. In this tutorial, we will look at different usage types of function print().

print() Function Syntax

The function print() provides advanced usage in order to print given message, string or text to the output or screen. The print operation can be done with different formats, output medium which makes print() function to accept multiple parameters.

print(OBJECTS, SEP=separator, END=end, FILE=file, FLUSH=flush)
  • OBJECTS are single or multiple objects which will be printed or output. OBJECT is generally a string, text, or message like “Hi”, “Input your name” etc. Even OBJECTS generally a single variable or value multiple variables or values can be provided too.
  • SEP parameter is used to provide a separator for the OBJECT content. The SEP parameter is optional. The default value for the SEP parameter is ” which is an empty string.
  • END parameter is used the specify what will be printed at the end of the given object. END parameter is optional and the default value of the END parameter is ‘\n’ which is linefeed generally called as the end of the line.
  • FILE parameter is used to specify the output method. The FILE parameter is optional. The default value of the FILE parameter is ‘sys.stdout’ which is default shell or console.
  • The FLUSH parameter is used to specify whether the output will be flushed or not. The FLUSH parameter is optional. Default FLUSH parameter value is False which means the output will not be flushed by default. FLUSH can take True or False Boolean values to enable or disable FLUSH configuration.
LEARN MORE  How To Install and Use Linux Minicom Command Tutorial with Examples?

Print Variables and Values

This is the simplest usage form of the printf function. We will create a variable named a which holds string "Hi Poftut" . We will print this string by providing in to print function.

a = "Hi Poftut"                 

print("Hi Poftut")


age = 36



Below we can see that different types of objects can be printed to the console or standard output. We can print variables, text, string, character, a number easily because all of them can be converted to the string automatically.

Print Variables and Values

Print Variables

We can use python variables in print function in string definition. We will  We will provide the variable names in curly braces like below.

a = "Hi Poftut"                                                                                                                       
print("My quote is {}").format(a)

The output will the value of a appended to the print function string. We provide the value with format function into print function. The output will be like below.

My quote is Hi Poftut

Print Multiple Strings or Variables

The function print() can output multiple strings, variables or objects in a single execution. We will just provides these multiple strings or variables or objects to the print() function as multiple parameters. In the following example we will print “I am” , “Poftut” , 43 with a single print function.

print("I am ","Poftut")
#Output is "I am  Poftut"

print("I am ","Poftut",43)
#Output is "I am  Poftut 43"

#Output is "1 2 3 43"
Print Multiple Strings or Variables

Print with Separator

SEPARATOR is used to put a given separator between multiple strings or objects during output. We will set the parameter separator value as “–“. If there is a single value/variable or object the separator will not be printed because the separator is put between two values.

print("I am",sep='--')
#Output is "I am"

print("I am","Poftut",sep='--')
#Output is "I am--Poftut"

print("I am","Poftut","How",sep='--')
#Output is "I am--Poftut--How"

print("I am","Poftut","How","are",sep='--')
#Output is "I am--Poftut--How--are"

print("I am","Poftut","How","are","you?",sep='--')
#Output is "I am--Poftut--How--are--you?"
Print with Separator

Put Spaces and Tabs

While using print function formatting is important. We can format the output by using format specifiers those are similar to the variable specifiers.

a = "Hi Poftut" 
print("My quote is {0:20} ???").format(a)

We will get following output where the variable a is spaces 20 character.

My quote is Hi Poftut            ???

Print Dictionary, Tuple etc.

We generally use different type of data structures in our applications. We can easily print these type of key and values pairs with print .

table = {'ismail': 4127, 'ahmet': 4098, 'elif': 8637678}

print('ismail: {0[ismail]:d}; ahmet: {0[ahmet]:d}; ''elif: {0[elif]:d}'.format(table))
Print Dictionary, Tuple etc.
Print Dictionary, Tuple etc.

Print List and Tuples

List and Tuple is two popular data format or structure used in Python. List and Tuple is used to store multiple items in a single variable or object. The function print() can be used to print given list or tuple like below. But keep in mind that the output format will be a tuple or list.

mytuple = ("I am","Poftut","How","are","you?")
#Output will be ('I am', 'Poftut', 'How', 'are', 'you?')

mylist = ["I am","Poftut","How","are","you?"]
#Output will be ['I am', 'Poftut', 'How', 'are', 'you?']
Print List and Tuples

Leave a Comment