Skip to content

S1Q2 · Check If Domain is .com or .in

⚡ Quick Reference

Function: is_dot_com_or_dot_in(domain: str) -> bool

Core idea: check if the string ends with ".com" or ".in" using str.endswith().

def is_dot_com_or_dot_in(domain):
    return domain.endswith(".com") or domain.endswith(".in")

Key rules: - Must end with ".com" or ".in" (the dot is part of the suffix) - "invalidcom"False (no dot before com) - str.endswith() accepts a tuple - can check both in one call


Problem Statement

Problem

Write a function is_dot_com_or_dot_in(domain) that returns True if the domain ends with .com or .in, and False otherwise.

Examples:

Input
"example.com"
Output
True
Input
"website.in"
Output
True
Input
"mydomain.org"
Output
False
Input
"invalidcom"
Output
False

Tracing all examples

domain endswith(".com") endswith(".in") Result
"example.com" True
"website.in" True
"example.org" False
"invalidcom" ❌ (no dot) False
"mywebsitein" ❌ (no dot) False

The dot is part of the suffix

".com" includes the dot - so "invalidcom" correctly returns False since it doesn't have a . before com. Always include the dot in the suffix string.


Solution approaches

def is_dot_com_or_dot_in(domain):
    return domain.endswith((".com", ".in"))

str.endswith() accepts a tuple of suffixes and returns True if the string ends with any of them. The cleanest single-expression solution.

def is_dot_com_or_dot_in(domain):
    return domain.endswith(".com") or domain.endswith(".in")
def is_dot_com_or_dot_in(domain):
    return domain[-4:] == ".com" or domain[-3:] == ".in"

Slice the last 4 characters for .com and last 3 for .in. Less readable than endswith() but shows the underlying idea.

is_dot_com_or_dot_in = lambda domain: domain.endswith((".com", ".in"))

Key takeaways

01

str.endswith() accepts a tuple

domain.endswith((".com", ".in")) checks both suffixes in a single call. This is more Pythonic than using or with two separate endswith() calls.

02

Always include the dot in the suffix

Searching for ".com" (with dot) correctly rejects "invalidcom". Searching for "com" (without dot) would incorrectly accept it.

03

endswith() vs in

".com" in domain would also match "my.company.org" since .com appears in the middle. Always use endswith() for suffix checks - not in.