Perfect choice.
We’ll now do what most Python courses never do properly.
This module is surgical, method-complete, interview-driven, and CPython-aware.
🔥 MODULE 1 — STRING MASTERY (Day 1 of Layer-2)
Strings: Internals, Methods, Pitfalls & Interview Patterns




🧠 MODULE GOAL (VERY IMPORTANT)
By the end of String Mastery, you will:
- Know every important string method
- Know when to use which method
- Avoid hidden O(n²) traps
- Solve string interview questions fluently
- Explain string behavior from CPython internals
This is coding interview readiness, not theory.
1️⃣ Python Strings — INTERNAL REALITY (RECAP, BUT DEEP)
What a Python string REALLY is
- Immutable sequence of Unicode code points
- Stored as a compact array (PEP 393)
- Length is cached
- Hash is cached after first computation
s = "hello"
Internally:
PyUnicodeObject
├── length
├── hash (cached)
├── kind (1/2/4 bytes per char)
└── data buffer
🧠 Immutability is why strings are safe as dict keys
2️⃣ Immutability — WHY IT MATTERS (Interview Gold)
s = "abc"
s += "d"
This does NOT modify s.
What happens:
- New string
"abcd"created - Old
"abc"discarded (or reused via interning) srebound
🔥 Performance trap
s = ""
for x in data:
s += x # ❌ O(n²)
Correct:
"".join(data) # ✅ O(n)
Interviewers LOVE this question.
3️⃣ STRING METHODS — COMPLETE & GROUPED
We will NOT list randomly.
We group by INTENT (this is how you remember them).
🔍 A. SEARCHING METHODS (VERY IMPORTANT)
find() vs index()
s = "hello world"
s.find("o") # 4
s.find("x") # -1
s.index("o") # 4
s.index("x") # ❌ ValueError
| Method | Behavior |
|---|---|
find | Safe, returns -1 |
index | Strict, raises error |
🧠 Use find in interviews unless exception is desired
startswith() / endswith()
s.startswith("he")
s.endswith("ld")
- Faster than slicing
- Accept tuples
s.startswith(("he", "hi"))
Used heavily in:
- Parsers
- URL handling
- File filters
count()
"banana".count("a") # 3
⚠️ Does NOT overlap:
"aaaa".count("aa") # 2, not 3
✂️ B. SPLITTING METHODS (INTERVIEW FAVORITES)
split() vs rsplit()
"a,b,c".split(",") # ['a','b','c']
"a,b,c".rsplit(",",1) # ['a','b','c']
Difference:
splitfrom leftrsplitfrom right
🔥 partition() vs split() (VERY IMPORTANT)
s = "user:123"
s.split(":") # ['user','123']
s.partition(":") # ('user',':','123')
Why partition is powerful:
- Always returns 3 parts
- No list allocation
- Safe unpacking
key, _, value = s.partition(":")
Interviewers LOVE this.
🔁 C. JOINING (ONE METHOD — VERY IMPORTANT)
join() (string method, NOT list method)
",".join(["a","b","c"])
Rules:
- Iterable must contain only strings
- Joiner is the string
❌ Common mistake:
["a","b"].join(",") # ❌
🔄 D. TRANSFORMATION METHODS
Case conversion
s.lower()
s.upper()
s.casefold() # strongest (Unicode-safe)
🧠 Use casefold() for case-insensitive comparisons
Whitespace removal
s.strip()
s.lstrip()
s.rstrip()
Interview trap:
" a b ".strip() # 'a b' (middle space preserved)
Replace
s.replace("a","b",1) # limit supported
Returns new string (immutability).
✅ E. VALIDATION METHODS
s.isdigit()
s.isalpha()
s.isalnum()
s.isspace()
s.islower()
s.isupper()
⚠️ Unicode-aware:
"²".isdigit() # True
Interviewers may ask this.
🧱 F. FORMATTING (MODERN PYTHON)
f-strings (BEST)
name = "Raj"
f"Hello {name}"
Why best:
- Fast
- Readable
- Evaluated at runtime
.format() (legacy but relevant)
"{} {}".format(a,b)
% formatting (avoid in new code)
"%s %d" % ("a", 1)
4️⃣ STRING INTERNING (ADVANCED BUT IMPORTANT)
a = "hello"
b = "hello"
a is b # Often True
Why?
- Python interns small literals
- Optimization, not guarantee
Never rely on is for strings.
5️⃣ STRING INTERVIEW QUESTIONS (CORE SET)
🔥 Q1. Reverse words in a string
"hello world python" → "python world hello"
Efficient solution:
" ".join(reversed(s.split()))
🔥 Q2. Check Anagram
"listen", "silent"
Best:
from collections import Counter
Counter(a) == Counter(b)
🔥 Q3. First non-repeating character
"swiss" → 'w'
Pattern:
- Frequency map
- Single pass
🔥 Q4. Longest substring without repeating characters
(Sliding window — VERY common)
🔥 Q5. Why += on strings is slow in loops?
Expected answer:
- Immutability
- New allocation each time
- O(n²) growth
🧠 STRING MENTAL CHECKLIST (MEMORIZE)
Before writing string code:
- Am I concatenating in a loop?
- Should I use
partitioninstead ofsplit? - Do I need casefold?
- Is Unicode relevant?
- Can this be done in one pass?
📝 MODULE 1 — DAY 1 ASSIGNMENT (MANDATORY)
1️⃣ Predict output (NO RUNNING)
s = "aaaa"
print(s.count("aa"))
2️⃣ Choose correct method (Explain WHY)
s = "user:password"
Extract user safely even if : is missing.
3️⃣ Coding
Write a function:
def is_palindrome(s):
# ignore spaces & case
🔜 NEXT (VERY IMPORTANT)
Module 1 will continue in 3 focused sessions:
- Day 2 → Advanced string interview problems (sliding window, hashing)
- Day 3 → Unicode edge cases + performance traps + 20 coding questions
👉 Say “CONTINUE STRING MASTERY – DAY 2” when ready.