Calling a function from a print statement is quite an easy task in Python Programming. It can be done when there is a simple function call, which also reduces the lines of code. In this article, we will learn how we can call a function from a print statement.
Calling a Function Inside print()
In this method, we can directly invoke a Python in-built function or a user-defined function through a print statement.
def total(num1, num2):
return num1 + num2
# directly calling a user-defined function
print(total(3, 5))
# directly calling an in-built function
print(pow(3, 2))
Output
8 9
Calling Multiple Functions
We can also call multiple functions from a single print statement. The example shows calling multiple functions in one print() statement.
import math
def area(radius):
return math.pi * radius**2
def circumference(radius):
return 2 * math.pi * radius
# multiple function call from print statement
print("Area of Circle:", area(5), "and Circumference:", circumference(5))
Output
Area of Circle: 78.53981633974483 and Circumference: 31.41592653589793
Calling Functions Without Return Value
If the function does not explicitly return a value (returns None), print() will output None after the function execution when called within a print() statement.
def greet(name):
print(f"Hello, {name}!")
# Call the function inside a print statement
print(greet("Alice"))
Output
Hello, Alice! None