These are the notes for Python Coding Questions, episode 13, the third round of string questions. All three questions come straight from real code, and each one has a built-in tool that almost works: capitalizing every word (where title() turns they're into They'Re and a hand-written fix crashes on double spaces), converting camelCase to snake_case (where a simple loop mangles acronyms like HTTPServer, and regular expressions with groups and backreferences fix it), and counting words (where one space inside split(" ") changes the answer from 3 to 8). Along the way you'll see why a slice never raises IndexError, what a raw string is and why re needs it, and the difference between split() and split(" ").
Every program was run with Python 3.13 (python3 main.py), the SyntaxPilot compiler's Python command. Every output below is real. Tracebacks show the full path to your file; here it is shortened to main.py.
Q1: Capitalize every word
Real-life example: a school is printing sports day certificates. The names come in small letters, like asha's team, and must print as Asha's Team. If the certificate comes out as Asha'S Team, with a capital S after the apostrophe, someone gets a reprint. And a name typed with extra spaces, like ravi kumar, must keep its spacing. The rule: change only the first letter of each word. Don't touch the rest, and don't touch the spaces.
| Input | Output |
|---|---|
"they're bill's friends" |
"They're Bill's Friends" |
"chai is hot" (extra spaces) |
"Chai Is Hot" (spacing kept) |
"hello " (trailing space) |
"Hello " |
"" (empty) |
"" |
"visit iPhone store" |
"Visit IPhone Store" (the rest is left as typed) |
Ask the interviewer: what counts as a word: is they're one word? Must the spacing stay exactly as typed? Should the rest of each word be left as typed (iPhone → IPhone) or lowered (Iphone)?
Attempt 1: s.title() and s.capitalize()
Python strings have a title() method, which promises "title case". Title case means the first letter of every word is a capital. There's also capitalize(). Let's try both.
s = "they're bill's friends from the uk"
print(s.title())
print(s.capitalize())
print("hello world".title())
Output:
They'Re Bill'S Friends From The Uk
They're bill's friends from the uk
Hello World
Why it fails:
title()givesThey'ReandBill'S. It starts a new word after any character that isn't a letter, and the apostrophe isn't a letter. It also lowers every other letter, soukbecameUk, notUK.capitalize()only does the first letter of the whole string. It's meant for sentences, not words. (It also lowers everything else.)- With
"hello world",title()does keep the spaces. The spacing is not its problem; the apostrophes are.
Here are more examples of where title() thinks a word starts:
for s in ["asha's team", "visit iPhone store", "mother-in-law", "route 66b", "ravi kumar"]:
print(repr(s), "->", repr(s.title()))
Output:
"asha's team" -> "Asha'S Team"
'visit iPhone store' -> 'Visit Iphone Store'
'mother-in-law' -> 'Mother-In-Law'
'route 66b' -> 'Route 66B'
'ravi kumar' -> 'Ravi Kumar'
repr() shows a value the way you'd type it in code, with quotes, so spaces are visible. The certificate bug is right there: Asha'S. After a hyphen (Mother-In-Law) and even after a digit (66B) title() starts a new "word". And iPhone lost its capital P: Iphone. The same two rules explain everything: a new word after any non-letter, and lowercase for the rest.
print("iPhone and UPI".capitalize())
print("UK".title(), "McDonald".title())
Output:
Iphone and upi
Uk Mcdonald
An acronym (a word made from first letters, like UK, UPI or HTTP) is destroyed by both methods, because they lower the rest of each word.
Attempt 2: split(" ") and w[0].upper()
So we split into words ourselves. Split on a space, upper the first letter of each word with w[0].upper(), add the rest unchanged (w[1:]), and join with spaces again.
def cap_words(s):
return " ".join(w[0].upper() + w[1:] for w in s.split(" "))
print(cap_words("they're bill's friends"))
print(repr(cap_words("chai is hot")))
print(cap_words("hello "))
Output:
They're Bill's Friends
Traceback (most recent call last):
File "main.py", line 5, in <module>
print(repr(cap_words("chai is hot")))
~~~~~~~~~^^^^^^^^^^^^^^^^^^
File "main.py", line 2, in cap_words
return " ".join(w[0].upper() + w[1:] for w in s.split(" "))
~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "main.py", line 2, in <genexpr>
return " ".join(w[0].upper() + w[1:] for w in s.split(" "))
~^^^
IndexError: string index out of range
The apostrophes are fine now: They're Bill's Friends. But the second test crashes with IndexError: string index out of range. An IndexError means you asked for a position that doesn't exist. (<genexpr> in the traceback is the generator expression inside join(...): the w[0].upper() + w[1:] for w in ... part, which produces one value at a time.)
Why it fails: look at what split(" ") really gives, and what happens with an empty string:
print("chai is hot".split(" "))
w = ""
print(repr(w[:1]), repr(w[1:]), repr(w[:1].upper()))
print(w[0])
Output:
['chai', '', 'is', '', '', 'hot']
'' '' ''
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(w[0])
~^^^
IndexError: string index out of range
split(" ") cuts at every single space. Between two spaces in a row there is nothing, so it keeps an empty string '' there. Two spaces give one empty "word", three spaces give two. An empty string has no index 0, so w[0] crashes. The same happens for "hello " (a trailing space leaves an empty piece at the end) and for "" itself ("".split(" ") is ['']).
But look at the second line of output: w[:1] on the empty string is just '', no error.
The fix: w[:1].upper() + w[1:]
A slice takes part of a sequence: s[start:stop], from start up to (not including) stop. Unlike indexing, a slice never raises IndexError: if the positions don't exist, you just get whatever fits, which may be ''.
w[:1]is "the first letter, if there is one":'c'for"chai",''for"".w[1:]is "everything after the first letter":'hai'for"chai",''for"".
The empty pieces from split(" ") are actually useful: when we join them back with " ", the original spacing returns exactly.
import string
def cap_words(s):
return " ".join(w[:1].upper() + w[1:] for w in s.split(" "))
print(cap_words("they're bill's friends"))
print(repr(cap_words("chai is hot")))
print(repr(cap_words("hello ")), repr(cap_words("")))
print(cap_words("visit iPhone store"))
print(string.capwords("they're bill's friends"))
Output:
They're Bill's Friends
'Chai Is Hot'
'Hello ' ''
Visit IPhone Store
They're Bill's Friends
- Apostrophes:
They're Bill's Friends. - Spacing kept: two spaces and three spaces, exactly as typed.
- Trailing space and empty string: no crash.
iPhone→IPhone: it keeps its capital P, because we never lower the rest.title()would have writtenIphone. WhetherIPhoneis right is a good question to ask the interviewer.string.capwords(from thestringmodule) also gets the apostrophes right, but it squeezed the two spaces between they're and bill's into one. Fine for display, wrong if the spacing must stay.
Step by step
"chai is hot", printing each piece:
s = "chai is hot"
out = []
for w in s.split(" "):
new = w[:1].upper() + w[1:]
print(f"w={w!r:7} w[:1]={w[:1]!r:5} w[1:]={w[1:]!r:6} -> {new!r}")
out.append(new)
print(repr(" ".join(out)))
Output:
w='chai' w[:1]='c' w[1:]='hai' -> 'Chai'
w='' w[:1]='' w[1:]='' -> ''
w='is' w[:1]='i' w[1:]='s' -> 'Is'
w='hot' w[:1]='h' w[1:]='ot' -> 'Hot'
'Chai Is Hot'
(!r in an f-string means "show the repr", and :7 pads it to 7 characters so the columns line up.) The empty piece stays empty, and join puts a space on each side of it, which rebuilds the two spaces.
Piece w |
w[:1].upper() |
w[1:] |
New piece |
|---|---|---|---|
'chai' |
'C' |
'hai' |
'Chai' |
'' |
'' |
'' |
'' (no crash) |
'is' |
'I' |
's' |
'Is' |
'hot' |
'H' |
'ot' |
'Hot' |
string.capwords: what it really does
import string
s = " visit iPhone STORE "
print(repr(string.capwords(s)))
print(repr(string.capwords(s, " ")))
print(repr(" ".join(w.capitalize() for w in s.split())))
Output:
'Visit Iphone Store'
' Visit Iphone Store '
'Visit Iphone Store'
string.capwords(s) is exactly " ".join(w.capitalize() for w in s.split()): split on whitespace (dropping the extra spaces and the ones at the ends), capitalize() each word, join with one space. So it lowers the rest (Iphone, Store) and collapses the spacing. Give it a separator, capwords(s, " "), and it splits on single spaces like our fix, so the spacing stays; but it still lowers the rest.
- Complexity of the fix: one pass to split, one over the pieces, one to join: O(n) time and O(n) space for the pieces and the new string (it has to exist; it's the answer).
Q2: camelCase and snake_case
Real-life example: a train ticket prints RajdhaniExpress as one word, with a capital at the start of each word. That style is camelCase: no spaces, and the capitals stick up like a camel's humps. JavaScript and Java name variables this way (userName). The station board writes it rajdhani_express: small letters with an underscore between words. That's snake_case, the Python style (user_name). When a JavaScript app sends Python its data (as JSON), userName has to become user_name: copy the ticket onto the board, every word, nothing lost.
| Input (camelCase) | Output (snake_case) |
|---|---|
"userName" |
"user_name" |
"totalPriceInRupees" |
"total_price_in_rupees" |
"UserName" (capital first) |
"user_name" (no leading _) |
"HTTPServer" (acronym) |
"http_server" |
"getID2Card" |
"get_id2_card" |
"already_snake" |
"already_snake" (unchanged) |
Ask the interviewer: can a name start with a capital (that style, UserName, is often called PascalCase)? How should acronyms like HTTP or ID be handled? Do digits belong to the word before them? Do I also need the reverse, snake_case to camelCase?
Attempt: an underscore before every capital
The obvious idea: every capital letter starts a new word. Put an underscore before it, and lower it.
def to_snake(name):
out = ""
for ch in name:
if ch.isupper():
out += "_" + ch.lower()
else:
out += ch
return out
for name in ["userName", "totalPriceInRupees", "UserName",
"HTTPServer", "getID2Card"]:
print(name, "->", to_snake(name))
Output:
userName -> user_name
totalPriceInRupees -> total_price_in_rupees
UserName -> _user_name
HTTPServer -> _h_t_t_p_server
getID2Card -> get_i_d2_card
The first two are perfect. Then:
UserName→_user_name: a leading underscore.HTTPServer→_h_t_t_p_server: the acronym is torn into single letters.getID2Card→get_i_d2_card:IandDare split apart.
Why it fails: a capital doesn't always start a new word. At the very start, there's no word before it to separate from. And inside an acronym like HTTP, the capitals belong together. The rule has to look at the neighbours of each capital, not just at the capital itself. (Also, out += ... in a loop builds a new string every time, because strings are immutable; a list and one join is better. Episode 11 explains why.)
The rule: look at the neighbours
A new word starts:
- at a capital that comes right after a small letter or a digit: the
NinuserName, theCin2Card; - or at the last capital of a run of capitals, when a small letter follows it: the
SinHTTPServer.
A regular expression (regex for short) is a small pattern language for describing pieces of text like that. Python's re module runs them. The pieces we need:
| Piece | Meaning | Matches |
|---|---|---|
[A-Z] |
a character class: any one character from the set inside [ ]; A-Z is a range |
N, S, but not n |
[a-z0-9] |
one small letter or one digit | r, 2 |
+ |
a quantifier: one or more of the thing before it | [A-Z]+ matches HTTP |
( ... ) |
a group: remembers what that part matched | ([a-z]) remembers r |
\1, \2 |
a backreference in the replacement: "put back what group 1 / group 2 matched" | \1_\2 → r_N |
So rule 1 is the pattern ([a-z0-9])([A-Z]), replaced by \1_\2. Rule 2 is ([A-Z]+)([A-Z][a-z]), also replaced by \1_\2: group 1 is the acronym, group 2 is the capital that starts the next word plus its small letter.
re.findall and re.sub
re.findall(pattern, s)returns a list of every match of the pattern ins.re.sub(pattern, replacement, s)substitutes: it returns a new string where every match is replaced.
import re
print(re.findall(r"[A-Z]", "userNameIsAsha"))
print(re.sub(r"([a-z])([A-Z])", r"\1_\2", "userNameIsAsha"))
Output:
['N', 'I', 'A']
user_Name_Is_Asha
findall gives the capitals: N, I, A. And sub puts an underscore exactly at each word boundary: rN becomes r_N, eI becomes e_I, sA becomes s_A. The letters themselves come back through \1 and \2, unchanged; lowering comes later.
Here's what the groups hold, using re.search (which finds the first match and returns a match object):
import re
m = re.search(r"([a-z0-9])([A-Z])", "userName")
print(m.group(0), m.group(1), m.group(2), m.start())
print(re.findall(r"([a-z0-9])([A-Z])", "getID2Card"))
print(re.findall(r"[A-Z]+", "getID2CardHTTP"))
print(re.findall(r"[a-z]+", "userNameIsAsha"))
Output:
rN r N 3
[('t', 'I'), ('2', 'C')]
['ID', 'C', 'HTTP']
['user', 'ame', 's', 'sha']
group(0)is the whole match,rN;group(1)isr,group(2)isN;start()is where it begins (index 3).- With groups in the pattern,
findallreturns tuples of the groups:('t', 'I')and('2', 'C'). Those are the two word boundaries ingetID2Card. [A-Z]+grabs a whole run of capitals at once:IDandHTTPstay together. That's what the+is for.[a-z]+finds the runs of small letters, which shows the capitals are the "gaps" between them.
Raw strings: why the r
The r before the quotes makes a raw string: backslashes are kept as they are. In a normal string, Python itself treats a backslash as the start of an escape sequence (like \n for a new line), and \1 means "the character with code 1", an invisible control character.
import re
print(len("\1_\2"), repr("\1_\2"))
print(len(r"\1_\2"), repr(r"\1_\2"))
print(repr(re.sub("([a-z])([A-Z])", "\1_\2", "userName")))
Output:
3 '\x01_\x02'
5 '\\1_\\2'
'use\x01_\x02ame'
Without the r, the replacement is 3 characters: code 1, _, code 2. The re module never sees a backslash, so it puts in those invisible characters instead of the groups, and rN disappears: 'use\x01_\x02ame'. No error, just garbage. With the r, it's 5 characters, \, 1, _, \, 2 (the repr shows each backslash doubled), and re reads them as backreferences. Always write regex patterns and replacements as raw strings.
One rule isn't enough
What if we use only the "small letter then capital" rule?
import re
for name in ["HTTPServer", "getID2Card"]:
print(name, "->", re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", name).lower())
Output:
HTTPServer -> httpserver
getID2Card -> get_id2_card
getID2Card is now right, but HTTPServer has no small letter before a capital anywhere (PS is capital-capital), so nothing is split: httpserver. That's why the acronym rule is needed.
The fix: two re.sub rules, then lower()
import re
def to_snake(name):
name = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", name) # acronyms
name = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", name) # aB -> a_B
return name.lower()
for name in ["userName", "totalPriceInRupees", "UserName",
"HTTPServer", "getID2Card", "already_snake"]:
print(name, "->", to_snake(name))
Output:
userName -> user_name
totalPriceInRupees -> total_price_in_rupees
UserName -> user_name
HTTPServer -> http_server
getID2Card -> get_id2_card
already_snake -> already_snake
Every name is correct. UserName has no leading underscore (the U has nothing before it, so neither rule matches there). HTTPServer → http_server. getID2Card → get_id2_card (the digit stays with ID). A name already in snake_case has no capitals, so nothing matches and it's left alone.
Step by step
What each rule does to each name:
import re
for name in ["HTTPServer", "getID2Card", "UserName", "parseJSONData"]:
a = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", name)
b = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", a)
print(f"{name:13} rule 1: {a:14} rule 2: {b:15} lower: {b.lower()}")
Output:
HTTPServer rule 1: HTTP_Server rule 2: HTTP_Server lower: http_server
getID2Card rule 1: getID2Card rule 2: get_ID2_Card lower: get_id2_card
UserName rule 1: UserName rule 2: User_Name lower: user_name
parseJSONData rule 1: parseJSON_Data rule 2: parse_JSON_Data lower: parse_json_data
| Name | Acronym rule ([A-Z]+)([A-Z][a-z]) |
Small-then-capital rule ([a-z0-9])([A-Z]) |
lower() |
|---|---|---|---|
HTTPServer |
group 1 HTTP, group 2 Se → HTTP_Server |
no match | http_server |
getID2Card |
no match (ID is followed by 2, not a capital + small letter) |
tI → t_I, 2C → 2_C |
get_id2_card |
UserName |
no match | rN → r_N |
user_name |
parseJSONData |
group 1 JSON, group 2 Da → JSON_Data |
eJ → e_J |
parse_json_data |
Notice in the acronym rule that [A-Z]+ is greedy (it grabs as many capitals as it can) but still leaves one capital for [A-Z][a-z]: HTTPS would be too many, so the regex backtracks to HTTP and lets Se match the second group.
More edge cases:
import re
def to_snake(name):
name = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", name)
name = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", name)
return name.lower()
for name in ["userID", "IOError", "ABC", "a", "", "version2Beta", "pdfToHTML"]:
print(repr(name), "->", repr(to_snake(name)))
Output:
'userID' -> 'user_id'
'IOError' -> 'io_error'
'ABC' -> 'abc'
'a' -> 'a'
'' -> ''
'version2Beta' -> 'version2_beta'
'pdfToHTML' -> 'pdf_to_html'
An acronym at the end (userID, pdfToHTML) is handled by the small-then-capital rule; an acronym at the start (IOError) by the acronym rule. An all-capitals name stays one word, and the empty string is fine.
- Complexity: each
re.subwalks the name once: O(n) time, and the new string is O(n) space.
Going back: snake_case to camelCase
Going back is easier. Split on underscores, keep the first word as it is, and title-case the others.
def to_camel(name):
first, *rest = name.split("_")
return first + "".join(w.title() for w in rest)
for name in ["user_name", "total_price_in_rupees",
"http_server", "x", "order__id"]:
print(name, "->", to_camel(name))
Output:
user_name -> userName
total_price_in_rupees -> totalPriceInRupees
http_server -> httpServer
x -> x
order__id -> orderId
first, *rest = ... is extended unpacking: the first item goes into first, and all the rest (possibly none) go into the list rest.
first, *rest = "total_price_in_rupees".split("_")
print(repr(first), rest)
first, *rest = "x".split("_")
print(repr(first), rest)
print("order__id".split("_"))
print(repr("".title()), "price".title(), "id2card".title(), "x2y".title())
Output:
'total' ['price', 'in', 'rupees']
'x' []
['order', '', 'id']
'' Price Id2Card X2Y
- For
"x",restis empty, so the answer is justx. order__id(a double underscore) splits into['order', '', 'id']. The empty piece title-cases to''and just disappears:orderId.- Here
title()is safe, because snake_case words have no apostrophes. But note"id2card".title()isId2Card:title()still starts a "word" after the digit.
Going there and back does not always give the original name: snake_case has thrown away which letters were capitals.
import re
def to_snake(name):
name = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", name)
name = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", name)
return name.lower()
def to_camel(name):
first, *rest = name.split("_")
return first + "".join(w.title() for w in rest)
for name in ["userName", "totalPriceInRupees", "HTTPServer", "getID2Card"]:
print(name, "->", to_snake(name), "->", to_camel(to_snake(name)))
Output:
userName -> user_name -> userName
totalPriceInRupees -> total_price_in_rupees -> totalPriceInRupees
HTTPServer -> http_server -> httpServer
getID2Card -> get_id2_card -> getId2Card
HTTPServer comes back as httpServer, and getID2Card as getId2Card. Good to mention in an interview: the conversion loses information about acronyms.
- Complexity of
to_camel: O(n) time and O(n) space.
Q3: Count words
Real-life example: your English teacher says: an essay of at most 100 words. Before you submit, you count them. But you typed it fast on a phone: extra spaces, a space at the start, new lines. Spaces aren't words. And a list typed with commas, chai,samosa,jalebi, is three words, even with no spaces.
| Input | Output |
|---|---|
"chai is hot" |
3 |
" chai is hot " (extra spaces) |
3 |
"" |
0 |
" " (only spaces) |
0 |
"line one\nline\ttwo" (new line, tab) |
4 |
"chai,samosa,jalebi" |
3 |
"wait... what?!" |
2 |
Ask the interviewer: does punctuation split words (is chai,samosa one word or two)? Is they're one word? Is a number like 2 a word? What about tabs and new lines?
Attempt 1: len(s.split(" "))
The obvious way: split on a space and count the pieces.
def count_words(s):
return len(s.split(" "))
print(count_words("chai is hot"))
print(count_words(" chai is hot "))
print(count_words(""))
print(" chai is hot ".split(" "))
Output:
3
8
1
['', '', 'chai', '', '', 'is', 'hot', '']
Why it's wrong: 3 is correct, but 8 for the same three words with extra spaces, and 1 for an empty string. The pieces show why: splitting on one space keeps an empty string between every two spaces in a row, and at the start and the end.
s = " chai is hot "
for i, piece in enumerate(s.split(" ")):
print(i, repr(piece))
print("".split(" "), "".split(), " ".split())
Output:
0 ''
1 ''
2 'chai'
3 ''
4 ''
5 'is'
6 'hot'
7 ''
[''] [] []
| Pieces | Where they come from |
|---|---|
| 0, 1 | the two spaces at the start: nothing before the first, nothing between the two |
| 2 | chai |
| 3, 4 | the three spaces after chai leave two empty gaps |
| 5, 6 | is, hot |
| 7 | the trailing space: nothing after it |
Three real words and five empty strings: 8. And "".split(" ") is [''], a list with one empty string: length 1.
In Q1 those empty pieces were exactly what we wanted (they kept the spacing). Here they're the bug. Same tool, different job.
Attempt 2: len(s.split())
split() with no argument is a different tool. It cuts at any run of whitespace and throws away empty pieces, including at the start and end. Whitespace means spaces, tabs \t and new lines \n.
def count_words(s):
return len(s.split()) # no argument: any run of whitespace
print(" chai is hot ".split())
for s in ["chai is hot", " chai is hot ", "", " ", "line one\nline\ttwo"]:
print(repr(s), count_words(s))
print(count_words("chai,samosa,jalebi"), count_words("wait... what?!"))
Output:
['chai', 'is', 'hot']
'chai is hot' 3
' chai is hot ' 3
'' 0
' ' 0
'line one\nline\ttwo' 4
1 2
Three for both sentences, zero for the empty string and for spaces only ("".split() is []), and four across the tab and the new line.
But chai,samosa,jalebi is 1 word, and wait... what?! is 2 only by luck (there happens to be a space). split() only knows whitespace, not punctuation.
The fix: describe a word with re.findall
Instead of describing what separates words, describe what a word is: a run of letters, digits and apostrophes. In regex: the character class [A-Za-z0-9'] (one capital, small letter, digit or apostrophe) and + (one or more). re.findall returns every match.
import re
def count_words(s):
return len(re.findall(r"[A-Za-z0-9']+", s))
for s in [" chai is hot ", "chai,samosa,jalebi", "wait... what?!",
"they're 2 friends", "", "!!!"]:
print(repr(s), count_words(s), re.findall(r"[A-Za-z0-9']+", s))
Output:
' chai is hot ' 3 ['chai', 'is', 'hot']
'chai,samosa,jalebi' 3 ['chai', 'samosa', 'jalebi']
'wait... what?!' 2 ['wait', 'what']
"they're 2 friends" 3 ["they're", '2', 'friends']
'' 0 []
'!!!' 0 []
Three for the spaces, three for chai, samosa, jalebi, two for wait what, and they're stays one word because the apostrophe is in the class. Empty, and three exclamation marks: zero.
Step by step
re.finditer gives each match as a match object, so you can see where every word starts and ends:
import re
for m in re.finditer(r"[A-Za-z0-9']+", "wait... they're here!"):
print(m.group(), m.start(), m.end())
print(re.findall(r"\w+", "they're 2 friends"))
print(re.findall(r"[A-Za-z0-9']+", "well-known chai_stall"))
Output:
wait 0 4
they're 8 15
here 16 20
['they', 're', '2', 'friends']
['well', 'known', 'chai', 'stall']
The regex scans left to right. At index 0 it matches wait (indexes 0 to 3; end() is one past the last). The dots and the space can't start a match, so it skips to index 8 and matches they're (the apostrophe is allowed). Then here; the ! is skipped.
Two warnings:
\w+is a common shortcut (\wmeans "a word character": letters, digits and underscore), but it has no apostrophe, sothey'rebecomes 2 words.-
With our class, the hyphen and the underscore split words:
well-knownis 2. Whether that's right is another question for the interviewer. -
Complexity: both
split()andre.findallare O(n) time: one pass over the text. The list of words is O(n) space. (sum(1 for _ in re.finditer(...))counts without building the list.) Which one is right? Ask whether punctuation should split words. For plain sentences,len(s.split())is the standard answer.
Common mistakes (and what you really see)
| Mistake | What you see | Why | Fix |
|---|---|---|---|
s.title() on names |
"they're" → They'Re, "bill's" → Bill'S |
a new word after any non-letter | w[:1].upper() + w[1:] per word |
s.title() / capitalize() on acronyms |
"uk" stays Uk, "iPhone" → Iphone, "UPI" → upi |
both lower the rest of each word | leave w[1:] unchanged |
s.capitalize() for every word |
They're bill's friends from the uk |
only the first letter of the whole string | split into words |
w[0].upper() for w in s.split(" ") |
IndexError: string index out of range |
double spaces give empty words, and ''[0] doesn't exist |
w[:1]: a slice never crashes |
string.capwords(s) when spacing matters |
"they're bill's" → They're Bill's (one space) |
it splits with split() and joins with one space |
split(" ") and join with " " |
"_" + ch.lower() before every capital |
UserName -> _user_name, HTTPServer -> _h_t_t_p_server |
not every capital starts a word | two re.sub rules, then lower() |
only the ([a-z0-9])([A-Z]) rule |
HTTPServer -> httpserver |
no small letter before the S |
add the acronym rule ([A-Z]+)([A-Z][a-z]) |
"\1_\2" without r |
'use\x01_\x02ame' (no error) |
\1 becomes control character 1 |
raw strings: r"\1_\2" |
using re without importing it |
NameError: name 're' is not defined. Did you forget to import 're'? |
re is a module, not a built-in |
import re |
len(s.split(" ")) |
" chai is hot " → 8, "" → 1 |
keeps empty pieces | len(s.split()) |
len(s.split()) on "chai,samosa,jalebi" |
1 | only whitespace splits | re.findall(r"[A-Za-z0-9']+", s) |
re.findall(r"\w+", s) to count words |
"they're" → ['they', 're'], 2 words |
\w has no apostrophe |
[A-Za-z0-9']+ |
Your turn: kebab-case (solution)
Web page addresses use kebab-case: small words joined by hyphens, like python-coding-questions. Turn " Python Coding Questions! " into "python-coding-questions", with no double hyphens, and no hyphen at the start or the end.
Solution
First, the two obvious attempts, so you can see what goes wrong:
s = " Python Coding Questions! "
print(repr(s.lower().replace(" ", "-")))
print(repr("-".join(s.lower().split())))
Output:
'--python-coding---questions!-'
'python-coding-questions!'
replace(" ", "-") turns every space into a hyphen: two at the start, three in the middle, one at the end. split() fixes the spacing (the Q3 lesson), but the ! is still there, because split() only knows whitespace.
You already know both tools you need: lower it, then describe a word with re.findall (the Q3 fix), and join the words with "-". Joining puts a hyphen only between words, never at the ends, and there are no empty words, so there can't be a double hyphen.
import re
def to_kebab(s):
words = re.findall(r"[a-z0-9]+", s.lower()) # runs of letters/digits
return "-".join(words)
for s in [" Python Coding Questions! ", "Hello, World!!", "C++ & Java 101",
"already-kebab-case", "---", ""]:
print(repr(s), "->", repr(to_kebab(s)))
Output:
' Python Coding Questions! ' -> 'python-coding-questions'
'Hello, World!!' -> 'hello-world'
'C++ & Java 101' -> 'c-java-101'
'already-kebab-case' -> 'already-kebab-case'
'---' -> ''
'' -> ''
Step by step for the example:
import re
s = " Python Coding Questions! "
print(repr(s.lower()))
print(re.findall(r"[a-z0-9]+", s.lower()))
print(repr("-".join(re.findall(r"[a-z0-9]+", s.lower()))))
Output:
' python coding questions! '
['python', 'coding', 'questions']
'python-coding-questions'
Because we lowered first, the class only needs a-z and 0-9. Spaces, !, +, & and existing hyphens are never part of a match, so they all just act as separators. Edge cases: a string of only punctuation gives ''. (Note C++ becomes c: the + signs are dropped. Ask whether that's acceptable.)
Without regex: turn every character that isn't a letter or digit into a space, then split() and join with hyphens.
def to_kebab(s):
cleaned = "".join(ch if ch.isalnum() else " " for ch in s.lower())
return "-".join(cleaned.split())
for s in [" Python Coding Questions! ", "Hello, World!!", "C++ & Java 101", "---", ""]:
print(repr(s), "->", repr(to_kebab(s)))
Output:
' Python Coding Questions! ' -> 'python-coding-questions'
'Hello, World!!' -> 'hello-world'
'C++ & Java 101' -> 'c-java-101'
'---' -> ''
'' -> ''
Bonus: camelCase input too. Insert a space (instead of _) with the two rules from Q2, then do the same:
import re
def to_kebab(s):
s = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1 \2", s) # Q2's two rules,
s = re.sub(r"([a-z0-9])([A-Z])", r"\1 \2", s) # with a space
return "-".join(re.findall(r"[a-z0-9]+", s.lower()))
for s in ["userName", "HTTPServer", " Python Coding Questions! "]:
print(repr(s), "->", repr(to_kebab(s)))
Output:
'userName' -> 'user-name'
'HTTPServer' -> 'http-server'
' Python Coding Questions! ' -> 'python-coding-questions'
All versions are O(n) time and O(n) space.
Practice questions (with solutions)
1. Count the sentences in a paragraph. "Hi Asha. How are you? I'm fine!!! Wait... really?" → 5. A sentence ends with ., ! or ? (or with the end of the text).
Solution
The trap: counting full stops.
text = "Hi Asha. How are you? I'm fine!!! Wait... really?"
print(text.count("."))
print(text.split("."))
Output:
4
['Hi Asha', " How are you? I'm fine!!! Wait", '', '', ' really?']
4 dots: one ends a sentence, and three are the ... of a single pause. And ? and ! aren't counted at all. Instead, split on a run of end marks with re.split (it splits wherever the pattern matches), and count the pieces that aren't empty or only spaces.
import re
def count_sentences(text):
pieces = re.split(r"[.!?]+", text)
return len([p for p in pieces if p.strip()])
text = "Hi Asha. How are you? I'm fine!!! Wait... really?"
print(re.split(r"[.!?]+", text))
for t in [text, "No full stop at the end", "", " ", "?!", "One. Two."]:
print(repr(t), count_sentences(t))
Output:
['Hi Asha', ' How are you', " I'm fine", ' Wait', ' really', '']
"Hi Asha. How are you? I'm fine!!! Wait... really?" 5
'No full stop at the end' 1
'' 0
' ' 0
'?!' 0
'One. Two.' 2
[.!?]+ treats !!! and ... as one separator (the + again). The text ends with ?, so the last piece is ''; p.strip() (which removes whitespace from both ends) is '' for empty or space-only pieces, and '' is falsy, so they're dropped. Text with no end mark still counts as one sentence. O(n) time, O(n) space.
Ask the interviewer about abbreviations: a dot doesn't always end a sentence.
import re
text = "Dr. Rao is here. Come in."
print(len([p for p in re.split(r"[.!?]+", text) if p.strip()]))
Output:
3
Two sentences, but Dr. makes it 3. Handling that needs a list of known abbreviations; mention it, and agree on the simple rule.
2. Longest word in a sentence, with ties. "chai and samosa beside jalebi" → all three 6-letter words, ['samosa', 'beside', 'jalebi']. Punctuation is not part of a word.
Solution
The traps: punctuation sticks to words after split(), and max returns only one word on a tie.
s = "I like chai, samosa, jalebi!"
print(max(s.split(), key=len))
print([(w, len(w)) for w in s.split()])
Output:
samosa,
[('I', 1), ('like', 4), ('chai,', 5), ('samosa,', 7), ('jalebi!', 7)]
samosa, with the comma counted as a letter (7), and jalebi! ties with it but is lost. max(..., key=len) compares the words by their length and returns the first largest one. Use the Q3 regex to get clean words, find the best length, then keep every word of that length:
import re
def longest_words(s):
words = re.findall(r"[A-Za-z0-9']+", s)
if not words:
return []
best = max(len(w) for w in words)
return [w for w in words if len(w) == best]
for s in ["I like chai, samosa, jalebi!", "chai and samosa beside jalebi",
"Python is fun", "a b c", ""]:
print(repr(s), "->", longest_words(s))
print(max("chai and samosa beside jalebi".split(), key=len))
Output:
'I like chai, samosa, jalebi!' -> ['samosa', 'jalebi']
'chai and samosa beside jalebi' -> ['samosa', 'beside', 'jalebi']
'Python is fun' -> ['Python']
'a b c' -> ['a', 'b', 'c']
'' -> []
samosa
The if not words check matters: max() of an empty sequence raises ValueError. The last line shows plain max keeping only the first of the tie, which is fine if the interviewer asks for "the first longest word". Two passes, O(n) time, O(n) space.
One pass: keep the best length so far and the words with it. A longer word starts a new list; an equal one joins it.
import re
def longest_words(s):
best, words = 0, []
for w in re.findall(r"[A-Za-z0-9']+", s):
if len(w) > best:
best, words = len(w), [w] # a new record: start again
elif len(w) == best:
words.append(w) # a tie: keep it too
return words
print(longest_words("chai and samosa beside jalebi"))
print(longest_words("I like chai, samosa, jalebi!"))
Output:
['samosa', 'beside', 'jalebi']
['samosa', 'jalebi']
3. Capitalize only the first word of each sentence. "hello there. how are you? i use an iPhone! ok" → "Hello there. How are you? I use an iPhone! Ok". Leave everything else exactly as typed.
Solution
The built-ins fail in two different ways:
s = "hello there. how are you? i use an iPhone! ok"
print(s.capitalize())
print(s.title())
Output:
Hello there. how are you? i use an iphone! ok
Hello There. How Are You? I Use An Iphone! Ok
capitalize() only does the very first letter (and lowers iPhone); title() does every word. Walk the characters with a flag (a True/False variable) that says "the next letter starts a sentence". It starts True; after ., ! or ? it's set to True again; the first letter after that is uppered and the flag goes back to False.
def capitalize_sentences(s):
out = []
cap_next = True # the very first letter starts a sentence
for ch in s:
if cap_next and ch.isalpha():
out.append(ch.upper())
cap_next = False
else:
out.append(ch)
if ch in ".!?":
cap_next = True # the next letter starts a new sentence
return "".join(out)
for s in ["hello there. how are you? i use an iPhone! ok",
" wait... what?! no way.", "", "already Fine. yes"]:
print(repr(s), "->", repr(capitalize_sentences(s)))
Output:
'hello there. how are you? i use an iPhone! ok' -> 'Hello there. How are you? I use an iPhone! Ok'
' wait... what?! no way.' -> ' Wait... What?! No way.'
'' -> ''
'already Fine. yes' -> 'Already Fine. Yes'
Spaces before a sentence are skipped (they aren't letters, so the flag waits), the double space is kept, and iPhone is untouched. Note Wait... What?!: the ... counts as a sentence end here, the same question as in practice 1. O(n) time, O(n) space for the list.
With regex: re.sub also accepts a function as the replacement; it's called with each match object and returns the replacement text.
import re
def capitalize_sentences(s):
return re.sub(r"(^\s*|[.!?]\s+)([a-z])",
lambda m: m.group(1) + m.group(2).upper(), s)
print(capitalize_sentences("hello there. how are you? i use an iPhone! ok"))
Output:
Hello there. How are you? I use an iPhone! Ok
^ means "the start of the string", \s is any whitespace character and * means "zero or more". So group 1 is either the start (plus any spaces) or an end mark plus spaces, and group 2 is the small letter after it.
4. Headline case: capitalize every word except small words. "the lord of the rings" → "The Lord of the Rings". Small words (a, an, the, of, in, on, and, or, to) stay small, except as the first or last word.
Solution
SMALL = {"a", "an", "the", "of", "in", "on", "and", "or", "to"}
def headline(s):
words = s.lower().split()
out = []
for i, w in enumerate(words):
if i == 0 or i == len(words) - 1 or w not in SMALL:
out.append(w[:1].upper() + w[1:])
else:
out.append(w)
return " ".join(out)
for s in ["the lord of the rings", "war and peace", "a tale of two cities",
"what are you looking at", "OF MICE AND MEN"]:
print(repr(s), "->", repr(headline(s)))
Output:
'the lord of the rings' -> 'The Lord of the Rings'
'war and peace' -> 'War and Peace'
'a tale of two cities' -> 'A Tale of Two Cities'
'what are you looking at' -> 'What Are You Looking At'
'OF MICE AND MEN' -> 'Of Mice and Men'
enumerate gives each word with its index, so we can spot the first and last. A set makes w not in SMALL an O(1) average lookup. We lower first here (titles in capitals must become Of Mice and Men), so this version does not keep acronyms; ask. The w[:1] slice from Q1 keeps it safe even if a word were empty. O(n) time, O(n) space.
5. Word frequency, ignoring case and punctuation. "Chai, chai, CHAI! Samosa and chai. Samosa?" → chai 4, samosa 2, and 1.
Solution
Lower the text, find the words with the Q3 regex, and count them with a Counter (from collections), a dictionary that counts things.
import re
from collections import Counter
def word_frequency(text):
return Counter(re.findall(r"[a-z0-9']+", text.lower()))
text = "Chai, chai, CHAI! Samosa and chai. Samosa?"
c = word_frequency(text)
print(c)
print(c.most_common(2))
print(c["jalebi"])
Output:
Counter({'chai': 4, 'samosa': 2, 'and': 1})
[('chai', 4), ('samosa', 2)]
0
Without the lower(), Chai, chai and CHAI would be three different keys; without the regex, chai, and chai. would be too. most_common(2) gives the two most frequent, and a missing word gives 0, not a KeyError. O(n) time, O(k) space for k different words.
Quick quiz
What does this print?
s = " a,b c "
print(len(s.split()), len(s.split(" ")), len(s.split(",")))
(A) 2 5 2 (B) 3 5 2 (C) 2 4 2 (D) 3 6 3
Answer
(A). Printing the pieces shows why:
s = " a,b c "
print(len(s.split()), len(s.split(" ")), len(s.split(",")))
print(s.split(), s.split(" "), s.split(","))
Output:
2 5 2
['a,b', 'c'] ['', 'a,b', '', 'c', ''] [' a', 'b c ']
s.split()cuts at runs of whitespace and drops empty pieces:a,bandc. The comma is not whitespace, soa,bstays one piece: 2.s.split(" ")cuts at every single space: empty (before the first space),a,b, empty (between the two spaces),c, empty (after the last space): 5.s.split(",")cuts at the comma:' a'and'b c ', with the spaces still inside: 2.
Interview questions
- Why does
"they're".title()giveThey'Re?title()starts a new word after any character that isn't a letter (apostrophes, hyphens, digits) and lowers every other letter. So it also breaks acronyms (uk→Uk) and mixed case (iPhone→Iphone). - What's the difference between
capitalize()andtitle()?capitalize()uppers only the first character of the whole string and lowers the rest;title()uppers the first letter of every "word" (by its own non-letter rule) and lowers the rest. - Capitalize every word while keeping the spacing?
" ".join(w[:1].upper() + w[1:] for w in s.split(" ")).split(" ")keeps empty pieces, so the join rebuilds the spacing, and the slicew[:1]doesn't crash on empty pieces. O(n) time and space. - Why does
w[0]raiseIndexErrorbutw[:1]doesn't? Indexing asks for one position that must exist; an empty string has none. A slice returns whatever fits in the range, so on""it's"". - What does
string.capwordsdo?" ".join(w.capitalize() for w in s.split()): right for apostrophes, but it lowers the rest of each word and collapses runs of spaces (unless you pass a separator). - Convert camelCase to snake_case? Two
re.subcalls:([A-Z]+)([A-Z][a-z])→\1_\2for acronyms (HTTPServer→HTTP_Server), and([a-z0-9])([A-Z])→\1_\2for normal boundaries, thenlower(). O(n). A loop that adds_before every capital gives_user_nameand_h_t_t_p_server. - What are groups and backreferences? Brackets
( )in a pattern make a group that remembers what it matched; in the replacement,\1,\2put back group 1 and group 2. In code,m.group(1)reads them. - Why use raw strings for regex? In a normal string, Python handles backslashes first:
"\1"becomes the control character with code 1, sorenever sees the backreference.r"\1"keeps the backslash. Write every pattern and replacement asr"...". - snake_case to camelCase?
first, *rest = name.split("_"), thenfirst + "".join(w.title() for w in rest). It doesn't round-trip acronyms:http_server→httpServer, notHTTPServer. split()vssplit(" ")?split()splits on any run of whitespace (spaces, tabs, new lines) and drops empty pieces, so"".split()is[].split(" ")splits on every single space and keeps empty pieces, so"".split(" ")is[''].- Count words when punctuation separates them?
len(re.findall(r"[A-Za-z0-9']+", s)): describe what a word is instead of what separates words. Ask whether apostrophes, hyphens and digits belong to words. re.findallvsre.subvsre.search?findallreturns a list of all matches (tuples of groups if the pattern has groups),subreturns a new string with every match replaced, andsearchreturns the first match object (orNone).
Glossary
| Term | Meaning | Example |
|---|---|---|
String (str) |
A sequence of characters: text | "chai is hot" |
| Index | A position in a string, from 0 | w[0] |
IndexError |
You asked for a position that doesn't exist | ""[0] → string index out of range |
| Slice | A part of a sequence, s[start:stop]; never raises IndexError |
""[:1] → '', "chai"[1:] → 'hai' |
| Empty string | A string with no characters; falsy | '' |
repr() |
A value shown as you'd type it, with quotes | repr("hello ") → 'hello ' |
| Title case | The first letter of every word a capital | They're Bill's Friends |
str.title() |
Uppers the first letter after any non-letter, lowers the rest | "they're".title() → They'Re |
str.capitalize() |
Uppers only the first character, lowers the rest | "iPhone and UPI" → Iphone and upi |
upper() / lower() |
Return an upper / lowercase copy | "c".upper() → 'C' |
isupper() / isalpha() / isalnum() |
True for a capital / a letter / a letter or digit | "N".isupper() → True |
| Acronym | A word made of first letters, written in capitals | UK, UPI, HTTP, ID |
string module |
Standard library module with string helpers | import string |
string.capwords |
capitalize() each word of s.split(), joined with one space |
capwords("a b") → 'A B' |
split(sep) |
Cut a string at every sep, keeping empty pieces |
"a b".split(" ") → ['a', '', 'b'] |
split() (no argument) |
Cut at any run of whitespace, dropping empty pieces | " a b ".split() → ['a', 'b'] |
| Whitespace | Spaces, tabs \t and new lines \n |
"line\ttwo" |
str.join |
Glue strings from an iterable, with the given string between them | "-".join(["a", "b"]) → 'a-b' |
strip() |
Remove whitespace from both ends | " ok ".strip() → 'ok' |
Generator expression (<genexpr>) |
Produces values one at a time, like a comprehension without the list | w[:1].upper() for w in words |
f-string !r, :7 |
Show the repr; pad to 7 characters |
f"{w!r:7}" |
| camelCase | Words joined with no spaces, each new word starting with a capital | userName, totalPriceInRupees |
| PascalCase | camelCase with a capital first letter too | UserName |
| snake_case | Small letters with underscores between words; the Python style | user_name |
| kebab-case | Small letters with hyphens between words; used in web addresses | python-coding-questions |
| Regular expression (regex) | A pattern that describes pieces of text | [A-Za-z0-9']+ |
re module |
Python's standard regular-expression module | import re |
re.findall |
A list of every match (tuples of groups if there are groups) | re.findall(r"[A-Z]", "userName") → ['N'] |
re.sub |
A new string with every match replaced (by text or a function) | re.sub(r"([a-z])([A-Z])", r"\1_\2", "userName") |
re.search |
The first match, as a match object (or None) |
re.search(r"[A-Z]", "userName") |
re.finditer |
Every match as a match object, one at a time | m.start(), m.end() |
re.split |
Split wherever the pattern matches | re.split(r"[.!?]+", text) |
| Match object | The result of a match: group(), start(), end() |
m.group(1) |
| Character class | [...]: any one character from the set; A-Z is a range |
[a-z0-9] |
Quantifier + |
One or more of the thing before it | [A-Z]+ matches HTTP |
Quantifier * |
Zero or more of the thing before it | \s* |
\w, \s, ^ |
A word character (letter, digit, _); a whitespace character; the start of the string |
\w+ splits they're |
| Greedy / backtracking | A quantifier takes as much as it can, then gives some back so the rest of the pattern can match | ([A-Z]+) gives back the S of HTTPServer |
| Group | ( ... ) in a pattern: remembers what that part matched |
([a-z])([A-Z]) has 2 groups |
| Backreference | \1, \2 in a replacement: put back group 1, group 2 |
r"\1_\2" |
| Escape sequence | A backslash code inside a normal string | "\n" new line, "\1" control character 1 |
| Control character | An invisible character with a small code | '\x01' |
| Raw string | r"...": backslashes are kept as typed |
r"\1" is 2 characters |
NameError |
A name was used that doesn't exist (for example, a module not imported) | name 're' is not defined |
| Extended unpacking | first, *rest = items: the first item, and a list of the others |
first, *rest = ["a", "b", "c"] |
| Flag | A True/False variable that remembers a state | cap_next |
lambda |
A small function written in one expression | lambda m: m.group(1).upper() |
max(..., key=len) |
The first item with the largest length | max(["ab", "cd"], key=len) → 'ab' |
ValueError |
A right type but a wrong value, e.g. max([]) |
max() iterable argument is empty |
enumerate |
Gives (index, value) pairs while looping |
for i, w in enumerate(words) |
| Set | A collection of unique items with O(1) average lookup | {"a", "of", "the"} |
Counter |
A dict that counts things; missing keys give 0 | Counter(["chai", "chai"]) |
| Immutable | Can never change after it's made; every string "change" builds a new string | out += ch makes a new string |
| O(n), O(1) | Time or space that grows like the input size n, or not at all | one pass over the text |
Summary
- Capitalize every word:
title()starts a word after every non-letter (They'Re,Bill'S) and lowers the rest (Uk,Iphone);capitalize()only does the first letter.w[0].upper()ons.split(" ")raisesIndexError: string index out of range, because double spaces make empty words." ".join(w[:1].upper() + w[1:] for w in s.split(" "))keeps the spacing and the rest as typed, since a slice never crashes.string.capwordscollapses spaces. O(n). - camelCase to snake_case: an underscore before every capital gives
_user_nameand_h_t_t_p_server. Look at the neighbours with twore.subrules,([A-Z]+)([A-Z][a-z])and([a-z0-9])([A-Z]), each replaced byr"\1_\2"(groups, backreferences, and a raw string), thenlower(). Back again:first, *rest = name.split("_")andtitle()the rest. O(n). - Count words:
len(s.split(" "))gives 8 for" chai is hot "and 1 for"", because it keeps empty pieces.len(s.split())splits on any whitespace and gives 3 and 0. When punctuation separates words, describe a word instead:len(re.findall(r"[A-Za-z0-9']+", s)). O(n).
Next episode: String questions IV: the longest substring without repeating characters with a sliding window, compressing aabccc into a2b1c3, and checking valid parentheses with a stack.