How To Extract Zip Code From Address In Python

In this tutorial, we will learn how to extract zip codes from an address using Python. Zip codes are important for various applications, such as geolocation, address verification, and data parsing. We will be using the re (regular expressions) module to achieve this task. By the end of this tutorial, you’ll be able to extract zip codes from a given address string.

Step 1: Importing the required library

First, we need to import the re module. This module provides support for regular expressions in Python.

Step 2: Write a function to extract the zip code

Next, we will create a function named extract_zip_code which takes an address string as input and returns the zip code.

The function uses the method, which searches for the first occurrence of the specified pattern in the input string. In this case, the pattern is a regular expression that matches a 5-digit zip code, optionally followed by a hyphen and 4 additional digits.

Step 3: Test the function

Now, let’s test the extract_zip_code function with some sample addresses.



The function correctly extracted the zip codes from the first two addresses and returned None for the third one, as no zip code was present in that address.

Full code

Here’s the complete code for this tutorial:


In this tutorial, we’ve learned how to extract zip codes from addresses using Python and the re module. This method can be easily integrated into applications that require processing and parsing address data for various purposes, such as geolocation, address validation, or data analysis.