Skip to content

Postcodes API

This is the API reference for all functions designed to be used on postcodes. You can find usage examples here.

hh.format_postcode

format_postcode(postcode: str, errors: str = 'raise') -> str | None

Attempts to clean a postcode to conform to UK standard.

Parameters:

Name Type Description Default
postcode str

Text you want to clean.

required
errors str

default = 'raise' which raises all errors. 'ignore' returns original value, 'coerce' attempts to turn postcode into string to run the function, if can't be run returns None.

'raise'

Raises:

Type Description
TypeError

Raised if postcode is not a string.

InvalidPostcodeError

Raised if postcode is not a valid length (5 to 7 chars).

Returns:

Type Description
str | None

Cleaned postcode.