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().
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:
"example.com"
True
"website.in"
True
"mydomain.org"
False
"invalidcom"
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¶
str.endswith() accepts a tuple of suffixes and returns True if the string ends with any of them. The cleanest single-expression solution.
Slice the last 4 characters for .com and last 3 for .in. Less readable than endswith() but shows the underlying idea.
Key takeaways¶
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.
Always include the dot in the suffix
Searching for ".com" (with dot) correctly rejects "invalidcom". Searching for "com" (without dot) would incorrectly accept it.
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.