How to Validate Indian Pincode Using Python

Published: (April 20, 2026 at 04:38 AM EDT)
2 min read
Source: Dev.to

Source: Dev.to

How to Validate Indian Pincode Using Python (Region, Sub‑Region and District Finder)

The following script validates a 6‑digit Indian pincode and returns the corresponding region, sub‑region, and district.

# Mapping of first digit to region
regions = {
    1: "Delhi",
    2: "Uttar Pradesh & Uttarakhand",
    3: "Rajasthan & Gujarat",
    4: "Maharashtra",
    5: "Andhra Pradesh & Telangana",
    6: "Tamil Nadu & Kerala",
    7: "West Bengal & North East",
    8: "Bihar & Jharkhand",
    9: "Army Postal Service"
}

# Mapping of first two digits to sub‑region
sub_regions = {
    11: "Delhi Region",
    40: "Mumbai Region",
    60: "Chennai Region",
    70: "Kolkata Region"
}

# Mapping of first three digits to district
districts = {
    110: "Delhi",          111: "Delhi (North)",      112: "Delhi (South)",
    400: "Mumbai",         401: "Mumbai (Suburban)",  402: "Mumbai (West)",
    600: "Chennai",        601: "Tiruvallur",         602: "Kanchipuram",
    700: "Kolkata",        701: "Kolkata (North)",    702: "Kolkata (South)"
}

def get_pincode_details(pincode: str) -> None:
    """
    Validate the pincode and print its region, sub‑region, and district.
    """
    if not pincode.isdigit() or len(pincode) != 6:
        print("Invalid pincode. Enter exactly 6 digits.")
        return

    num = int(pincode)
    first_digit = num // 100_000          # e.g., 4 for 400001
    first_two   = num // 10_000           # e.g., 40 for 400001
    first_three = num // 1_000            # e.g., 400 for 400001

    print("Region:     ", regions.get(first_digit,   "Unknown"))
    print("Sub‑region: ", sub_regions.get(first_two, "Unknown"))
    print("District:   ", districts.get(first_three, "Unknown"))

# Example usage
pincode = input("Enter 6‑digit Pincode: ").strip()
get_pincode_details(pincode)
0 views
Back to Blog

Related posts

Read more »

Pygame Snake, Pt. 3

Controlling the Square with Keyboard Input In Part 2 we had a square moving on a grid. Now we’ll make it respond to KEYDOWN events so the player can control it...

Pygame Snake, Pt. 2

Introduction In part 1 we set up a basic pygame window with a 1000 × 1000 pixel canvas and a 50 × 50 pixel square that moved continuously. For the snake game w...

Pygame Snake, Pt. 1

Introduction Pygame is a module that lets us create 2D games with Python. It’s a great way to learn programming concepts, and the classic game Snake makes an e...