ReviseAlgo Logo

Strings

String Methods

split, join, strip, replace, find

Interview: Essential string manipulation

Last Updated: June 12, 2026 8 min read

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 found
  • index(sub) — like find() but raises ValueError if not found
  • count(sub) — count non-overlapping occurrences
  • startswith(prefix) / endswith(suffix) — boolean checks

Transformation Methods

  • strip() / lstrip() / rstrip() — remove whitespace (or specified chars)
  • replace(old, new, count) — replace substrings
  • split(sep) / rsplit(sep, maxsplit) — split into list
  • join(iterable) — join list into string
  • lower() / upper() / title() / swapcase() — case transformation
  • casefold() — aggressive lowercase for case-insensitive comparison

Testing Methods

  • isdigit() / isalpha() / isalnum() — character type checks
  • isspace() / isidentifier() — whitespace and identifier checks
  • isupper() / islower() / istitle() — case checks

Formatting Methods

  • center(width, fillchar) — center-align with padding
  • ljust(width) / rjust(width) — left/right align
  • zfill(width) — pad with zeros
  • encode(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)