Python print() function
Lesson#9
Python print function is used to print something (text or values of variables) on screen.
x = 10
print("x: ", x)
# prints x: 10 on screen
Values of multiple variables can be printed with single print statement:
x = 10
y = 5
z = x + y
print("x: ", x, " y: ", y , " z: ", z)
# output
# x: 10 y: 5 z: 15
Formatted print()
Formatted print is another option available with print statement in which variables can be referred directly in single string:
The above example can be re-written using formatted print() with which the code look much cleaner as below:
x = 10
y = 5
z = x + y
print(f"x: {x} y: {y} z: {z}")
# output
# x: 10 y: 5 z: 15
Note the difference between x and {x}. x means x as string and {x} means value of variable x. So x prints x and {x} prints 10 (value of x).
It may be noted that formatted print starts with f before the string argument to print function.