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.
1 |
import re |
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.
1 2 3 4 5 6 |
def extract_zip_code(address: str) -> str: zip_code = re.search(r'\d{5}(?:-\d{4})?', address) if zip_code: return zip_code.group(0) else: return None |
The function uses the re.search()
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.
1 2 3 4 5 6 7 |
address1 = "123 Main St, Springfield, IL 62704-1234" address2 = "456 Elm St, Anytown, CA 90210" address3 = "789 Pine St, Someplace, NY" print(extract_zip_code(address1)) print(extract_zip_code(address2)) print(extract_zip_code(address3)) |
Output
62704-1234 90210 None
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import re def extract_zip_code(address: str) -> str: zip_code = re.search(r'\d{5}(?:-\d{4})?', address) if zip_code: return zip_code.group(0) else: return None address1 = "123 Main St, Springfield, IL 62704-1234" address2 = "456 Elm St, Anytown, CA 90210" address3 = "789 Pine St, Someplace, NY" print(extract_zip_code(address1)) print(extract_zip_code(address2)) print(extract_zip_code(address3)) |
Conclusion
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.