# UK address format, and the fields a parser returns for it

Source: https://parseaddr.com/uk-address-format

A UK address runs from the most specific line to the most general: the recipient, a company if there is one, the house number and street, a district, the town, an optional county, then the postcode on its own line. A house name goes on the line above the street. Written on one line, each line break becomes a comma ([Wikipedia, citing Royal Mail's guidance](https://en.wikipedia.org/wiki/Address#Address_format)).

Wikipedia's example, line by line, with the field `/v1/parse` returns for each line when the address after the name is sent as one string:

| Line | Example | Field returned |
| --- | --- | --- |
| Recipient | Mr A. Payne | not sent |
| Company or house name | ARAMARK Ltd. | `house: aramark ltd.` |
| Number and street | 30 Commercial Road | `house_number: 30, road: commercial road` |
| District | Fratton | `suburb: fratton` |
| Post town | PORTSMOUTH | `city: portsmouth` |
| County | Hampshire | `state_district: hampshire` |
| Postcode | PO1 1AA | `postcode: po1 1aa` |
| Country | UNITED KINGDOM | `country: united kingdom` |

## What /v1/parse returns for ten UK formats

Values come back in lower case and keep the input's spelling.

| Format | Input | Fields returned |
| --- | --- | --- |
| Number and street | `10 Downing Street, London SW1A 2AA` | `{"house_number": "10", "road": "downing street", "city": "london", "postcode": "sw1a 2aa"}` |
| Flat | `Flat 2, 14 High Street, Oxford OX1 4AG` | `{"unit": "flat 2", "house_number": "14", "road": "high street", "city": "oxford", "postcode": "ox1 4ag"}` |
| Lower case, no commas | `221b baker street london nw1 6xe` | `{"house_number": "221b", "road": "baker street", "city": "london", "postcode": "nw1 6xe"}` |
| Upper case | `25 DEANSGATE, MANCHESTER, M3 4LQ` | `{"house_number": "25", "road": "deansgate", "city": "manchester", "postcode": "m3 4lq"}` |
| Postcode with no space | `48 Princes Street Edinburgh EH22YJ` | `{"house_number": "48", "road": "princes street", "city": "edinburgh", "postcode": "eh22yj"}` |
| House name, no number | `Rose Cottage, Church Lane, Bampton, OX18 2LZ` | `{"house": "rose cottage", "road": "church lane", "city": "bampton", "postcode": "ox18 2lz"}` |
| PO box | `PO Box 61, Leeds LS1 6HD` | `{"po_box": "po box 61", "city": "leeds", "postcode": "ls1 6hd"}` |
| Country on the end | `19 Donegall Place, Belfast, BT1 5AB, United Kingdom` | `{"house_number": "19", "road": "donegall place", "city": "belfast", "postcode": "bt1 5ab", "country": "united kingdom"}` |
| Croydon (London-area post town) | `67 Park Lane, Croydon CR0 1JD` | `{"house_number": "67", "road": "park lane", "city_district": "croydon", "postcode": "cr0 1jd"}` |
| Business unit on an estate | `Unit 4, Riverside Industrial Estate, Station Road, Reading RG1 1LG` | `{"house_number": "unit 4", "road": "riverside industrial estate station road", "city": "reading", "postcode": "rg1 1lg"}` |

## Three cases to handle in your code

**A postcode with no space.** `EH22YJ` comes back as `eh22yj`. Every UK postcode ends in a digit and two letters, so put the space before the last three characters:

```python
import re

def uk_postcode(p):
    p = re.sub(r"\s+", "", p).upper()
    return p[:-3] + " " + p[-3:]

uk_postcode("eh22yj")  # "EH2 2YJ"
```

**Croydon and other London-area towns.** `67 Park Lane, Croydon CR0 1JD` returns `croydon` as `city_district`, not `city`. Croydon is its own post town: 60% of Greater London's area has postcodes outside the London post town, in 13 other areas such as CR, BR and KT ([Wikipedia](https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom)). Read `city` and fall back to `city_district`:

```python
town = parsed.get("city") or parsed.get("city_district")
```

**A unit on an estate.** `Unit 4, Riverside Industrial Estate, Station Road` returns `unit 4` as `house_number` and joins the estate and the street into one `road` value. If you store the estate name on its own, split it out of `road` yourself.

## UK postcode formats

A postcode is an outward code of two to four characters, a space, and an inward code of exactly three characters. There are six formats, where A is a letter and 9 is a digit ([Wikipedia](https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom)):

| Format | Example |
| --- | --- |
| `A9 9AA` | `M1 1AE` |
| `A99 9AA` | `B33 8TH` |
| `A9A 9AA` | `W1A 0AX` |
| `AA9 9AA` | `CR2 6XH` |
| `AA99 9AA` | `DN55 1PT` |
| `AA9A 9AA` | `EC1A 1BB` |

## Question

### How do I write a UK address on one line?

Keep the same order and put a comma where each line break was, for example 30 Commercial Road, Fratton, Portsmouth, PO1 1AA.

## Call it

```
curl -s https://api.parseaddr.com/v1/parse \
  -H "Authorization: Bearer $PARSEADDR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"address": "Flat 2, 14 High Street, Oxford OX1 4AG"}'
```

The free tier is 2,000 calls a month. See [pricing](https://parseaddr.com/pricing) for paid plans.
