Python Strings & Built-in Functions
Python Strings & Built-in Functions Interview Questions
isdigit(): Returns True if all characters are digits.
isalnum(): Returns True if all characters are alphanumeric.
Example: "abc".isalpha() = True, "123".isdigit() = True, "abc123".isalnum() = True.
CPython often optimizes repeated concatenation in some cases, but worst-case narrative remains O(n²) copies—''.join(parts) is the robust O(n) pattern.
Multiplication builds one new string efficiently—clear for fixed repetition.
Default split() splits on any run of whitespace and trims edges; splitting on a literal space is different.
" a b c ".split()
# ['a', 'b', 'c']
" a b c ".split(' ')
# ['', '', 'a', '', 'b', '', 'c', '', '']No—only leading/trailing characters in the optional charset (default whitespace).
find returns -1; index raises ValueError—choose for control flow vs exceptions.
Implementation-dependent interning—never rely on identity for string equality; always use ==.
Double the braces—single braces are expression boundaries in f-strings.
Yes—backslash and n; raw mainly affects backslash escaping, not quote rules edge cases at string end.
len counts Unicode code points—combining characters and emoji may surprise perceived “character” count; use unicodedata or grapheme libs if needed.
Decode bytes with explicit encoding (UTF-8 default in many contexts)—silent wrong decoding corrupts text; use errors= policy.
casefold is stronger for case-insensitive comparison across locales—prefer for matching; lower for display rules.
Tuple of prefixes tests any—cleaner than chained or calls.
partition always returns 3-tuple including separator—predictable when sep may be absent.
Single-element tuple needs trailing comma—otherwise (x) is just parentheses, not tuple, breaking formatting.
Replaces left-to-first count matches—order matters for overlapping patterns (non-overlapping scan).
s[:1000] won’t raise on short strings—clamps—unlike single-index access.
title capitalizes each word (naïve); capitalize only first char of whole string—both locale-insensitive simple rules.
Preserves \n/\r\n boundaries—line parsers needing delimiter info.
Removes one occurrence at start/end if present—safer than blind slice lengths.
Extra space favors left padding first when odd—document asymmetry if alignment matters in UI.
Fixed substring search uses optimized algorithms (e.g. Boyer-Moore-ish)—regex engine overhead unnecessary for literal needles.
Strictness ladder differs—Unicode fractions may be numeric but not decimal; interviews test awareness of edge Unicode classes.
Thousands separator per locale—readable large integers in formatted output.
Rebinding name to new object only—methods return new strings; explains safe dict keys if content frozen.
TypeError—map str first; generator expressions common for large sequences.
Bulk character replacement/delete faster than many replace calls—classic ASCII cleanup pipelines.
Falsy—if not s: catches empty; whitespace-only strings are truthy unless stripped.
Same visual glyph can encode multiple ways (NFC vs NFD)—security and duplicate detection bugs otherwise.
Removes any combination of leading chars from the set—not whole prefix substring—surprise for multi-char “prefix” expectations.
Adds placeholder and breaks at word boundaries—better UX than hard ellipsis slices.