← Python tutorial

Python

Functions

Define a function with def name(parameters):, followed by an indented block, and return a value if it should produce one.

Example: a function using an f-string

def greet(name):
    return f"Hello, {name}!"

print(greet("Chidi"))
Hello, Chidi!

The f before the opening quote makes it an f-string, anything inside curly braces gets evaluated and inserted directly into the string. This is the standard, readable way to build strings containing variables in modern Python, and is generally preferred over older approaches like manually joining strings with +.

Example
def greet(name):
    return f"Hello, {name}!"

print(greet("Chidi"))
Output
Hello, Chidi!
Ready to go further?

These free tutorials are a preview, our Backend Development cohort covers it in depth, with a real project and a cohort alongside you. Active

Enroll in Backend Development
Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account