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
+.
def greet(name):
return f"Hello, {name}!"
print(greet("Chidi"))
Hello, Chidi!