Strings
String Methods
split, join, strip, replace, find
Interview: Essential string manipulation
Python strings have a rich set of built-in methods for searching, transforming, and analyzing text. Since strings are immutable, all methods return new strings rather than modifying in place.
Searching Methods
find(sub)/rfind(sub)— returns index or -1 if not foundindex(sub)— like find() but raises ValueError if not foundcount(sub)— count non-overlapping occurrencesstartswith(prefix)/endswith(suffix)— boolean checks
Transformation Methods
strip()/lstrip()/rstrip()— remove whitespace (or specified chars)replace(old, new, count)— replace substringssplit(sep)/rsplit(sep, maxsplit)— split into listjoin(iterable)— join list into stringlower()/upper()/title()/swapcase()— case transformationcasefold()— aggressive lowercase for case-insensitive comparison
Testing Methods
isdigit()/isalpha()/isalnum()— character type checksisspace()/isidentifier()— whitespace and identifier checksisupper()/islower()/istitle()— case checks
Formatting Methods
center(width, fillchar)— center-align with paddingljust(width)/rjust(width)— left/right alignzfill(width)— pad with zerosencode(encoding)— convert to bytes
Use Cases
Parsing CSV, log files, and structured text data
Cleaning user input (strip whitespace, normalize case)
Building formatted output for reports and CLI tools
Text analysis and natural language preprocessing
Common Mistakes
Using split() without a separator argument when you need split on specific character
Forgetting that split() returns strings — need to convert to int/float manually
Using lower() instead of casefold() for case-insensitive Unicode comparison
Not using partition() when you need exactly 3 parts (before, separator, after)