Breaking out of a loop PREMIUM

Series: Looping
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
5 min. read 3 min. video Python 3.8—3.12

Let's talk about breaking out of loops in Python.

Exiting a loop early with return

Here's a function that uses a for loop to loop over an iterable of strings, and returns a value from within that loop if it finds a string that contains a given substring. If it doesn't find a match, it returns None:

...

A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.