Can `print` and `return` be used together?

0109

Yes, print and return can be used together in a function. You can use print to display information to the console and return to send a value back to the caller. Here's an example:

def greet(name):
    message = f"Hello, {name}!"
    print(message)  # This will print the message to the console
    return message  # This will return the message to the caller

result = greet("Alice")  # Calls the function
print(result)  # Output: Hello, Alice!

In this example, the function greet prints a greeting message and also returns it. The returned value can be stored in a variable or used elsewhere in the code.

0 Comments

no data
Be the first to share your comment!