Different Ways To Use Python Print Function
Python is very expressive language which provides a lot of different output types and extensions. Printf
is one of the most used function to print output. In this tutorial we will look different usage types of printf
Print String Value
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.
1 2 3 4 5 |
#!/bin/python3 a = "Hi Poftut" print(a) |
We will create a python source file named mytest.py
and write download code and run it.

Use 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.
1 2 3 4 5 |
#!/bin/python3 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.
1 |
My quote is Hi Poftut |
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.
1 2 3 4 5 |
#!/bin/python3 a = "Hi Poftut" print("My quote is {0:20} ???").format(a) |
We will get following output where the variable a
is spaces 20 character.
1 |
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
.
1 2 3 |
table = {'ismail': 4127, 'ahmet': 4098, 'elif': 8637678} print('ismail: {0[ismail]:d}; ahmet: {0[ahmet]:d}; ''elif: {0[elif]:d}'.format(table)) |
