ISO 3166-1 alpha-2 Country Code in PY
Validate 2-letter ISO 3166-1 alpha-2 country codes (US, GB, FR, JP, etc.) — structural check only.
Try it in the PY tester →Pattern
regexPY
^[A-Z]{2}$Python (re) code
pyPython
import re
pattern = re.compile(r"^[A-Z]{2}$")
input_text = "US"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
How the pattern works
^[A-Z]{2}$ requires exactly two uppercase letters anchored to the full string. Like the currency-code pattern, this verifies FORMAT not membership — combine with the official list to ensure the code actually exists.
Examples
Input
USMatches
US
Input
GBMatches
GB
Input
USANo match
—