US State Meta Information
The 'us' library provides an easy way to work with US state and territory meta information, offering data such as names, abbreviations, FIPS codes, and contiguous status. It also includes functions for looking up states by various criteria. The current version is 3.2.0, and it generally follows an active release cadence, with updates for Python version compatibility and dependency upgrades.
Warnings
- breaking Python 3.6 and 3.7 support has been officially dropped starting with 'us' version 3.2.0. Projects using these older Python versions must upgrade their Python interpreter or pin 'us' to a version prior to 3.2.0.
- breaking The `jellyfish` dependency, used for fuzzy matching in functions like `us.states.match()`, was upgraded to `jellyfish` 1.x in 'us' version 3.2.0. If your project has a direct dependency on an older `jellyfish` version, this upgrade might introduce conflicts or require adjustments.
- gotcha Functions like `us.states.match()` return a list of potentially matching state objects, even if only one match is found. Always account for this when processing the results, as direct access (e.g., `[0]`) might lead to `IndexError` if no matches are found.
- gotcha The `us.states.lookup()` function is more strict and returns `None` if no exact match is found (after an optional fuzzy search). Always check for `None` before accessing attributes of the returned object.
Install
-
pip install us
Imports
- us
import us
- states
import us.states
- territories
import us.territories
Quickstart
import us
# Access state objects directly
california = us.states.CA
print(f"California FIPS: {california.fips}")
# Look up states by name, abbreviation, or FIPS code
new_york = us.states.lookup('New York')
print(f"New York abbr: {new_york.abbr}")
# Fuzzy matching (requires 'jellyfish' library)
matched_states = us.states.match('Kanzus')
if matched_states:
print(f"Did you mean: {matched_states.name} ({matched_states.abbr})?")
# Access a list of all states and territories
print(f"Total states and territories: {len(us.states.all) + len(us.territories.all)}")
# Access special objects like 'unitedstatesofamerica'
usa = us.unitedstatesofamerica
print(f"USA name: {usa.name}, Birthday: {usa.birthday}")