This article shows basic usage of print() function.
Contents
- Basics of print function
- Arguments of print function
- How to display without line newline
- How to use format method
- How to use f-strings
sponsored link
Basics of print function
Numbers and strings can be displayed by using print function.
print("Apple")
# Apple
Difference between Python2 and Python3
Print function is written differently in python2 and python3.
print "Apple" #python2
print("Apple") #python3
In Python 2, it was a "print statement", but it was changed to a "print function" in Python 3.
sponsored link
Arguments of print function
I show arguments of print function.
Following code dares to show optional arguments.
import sys
print("Apple","Banana", sep=' ', end='\n', file=sys.stdout, flush=False)
#Apple Banana
With this notation, error will occur if "sys" is not imported.
Each argument is briefly explained below.
sep = ' ' : delimiter
The "sep" parameter is used to modify the delimiter character.
print("Apple", "Banana", "Orange") #delimiter : " " space
#Apple Banana Orange
print("Apple", "Banana", "Orange", sep=',') #delimiter : ", " comma
#Apple,Banana,Orange
print("Apple", "Banana", "Orange", sep=', ') #delimiter : ", " comma space
#Apple, Banana, Orange
end = '\n' : trailing character
The "end" parameter is used to modify the trailing character.
print("Apple", "Banana") #trailing character : "\n"
print("Orange", "Lemon") #trailing character : "\n"
#Apple Banana
#Orange Lemon
print("Apple", "Banana", end = ',') #trailing character : ", " comma
print("Orange", "Lemon", end = ',') #trailing character : ", " comma
#Apple Banana,Orange Lemon,
file = sys.stdout : save location
The "file" parameter is used to modify the trailing character.
The default "sys.stdout" is standard output.
The file parameter must be an object with a write method.
The output of print function can also be saved to a file.
In the example below, strings are output to a text file using print() function.
import os
dir_test = r'C:\Users\info\OneDrive\Desktop'
name_test = 'output.txt'
path_test = os.path.join(dir_test, name_test)
file_test = open(path_test, 'w')
print("Apple", "Banana", file=file_test)
file_test.close()
The text file is below.
### output.txt
Apple Banana
flush=False : flash operation
The "file" parameter is used to modify the trailing character.
If the print function is executed during heavy processing, the output of the print function may be displayed at once after the processing is finished.
If "flush=True", flush operation is enabled and the output of print function can be displayed immediately.
### Example of displaying processing progress
import time
for _ in range(10):
### --- Insert process here --- ###
print("*", end="", flush=True)
time.sleep(0.5)
#**********
The output of print function is displayed as processing progresses.
sponsored link
How to display without newline
I show how to display the output of print function without newline.
Change the "end" parameter to display output without newline.
In the example below, the trailing character is changed from "\n" to " ".
print("Apple", "Banana", end = ' ') #trailing character : " " space
print("Orange", "Lemon", end = ' ') #trailing character : " " space
#Apple Banana Orange Lemon
sponsored link
How to use "Format method"
"Format method" can be used to display variables embedded in string.
"Format method" is described as follow.
print( " string { } " .format( variable ) )
{ } is replaced by a variable.
x = 123
print('x : {}'.format(x))
#x : 123
If there are multiple variables, separate them with ",".
x = 123
y = 456
print('x : {}, y : {}'.format(x, y))
#x : 123, y : 456
If index number is specified for { }, it is replaced by the variable corresponding to index number.
x = "Variable"
y = 123
z = 456
print('{0} x : {1}, {0} y : {2}'.format(x, y, z) )
#Variable x : 123, Variable y : 456
If string is specified for { }, it is replaced by the variable corresponding to keyword.
a = "Apple"
b = "Banana"
print("{red} and {yellow}".format(red = a, yellow = b))
# Apple and Banana
sponsored link
How to use f-strings
Since Python 3.6, "f-strings" have been available.
It is a more concise way of writing than "Format method".
"f string" can be used by writing "f" or "F".
print( f"string { variable }" )
a = "Apple"
b = "Banana"
print(f"{a} and {b}")
#Apple and Banana
sponsored link