String Methods Reference
Print formatting, cleanups, splits, and substring substitutions.
Split string into list by separator.
Used In: CSV parsing, log parsing, tokenization.
str.split(sep=None, maxsplit=-1)'name,role,salary'.split(',')['name', 'role', 'salary']Time Complexity: O(N)
Common Mistakes: Calling split(',') on an empty string yields [''] rather than [].
Related Methods: partition(), splitlines()
split() vs partition(): split() splits by all occurrences of a separator and discards it; partition() splits at the first occurrence and returns a 3-tuple (head, sep, tail).
Concatenate list items with a separator.
Used In: Creating CSV rows, constructing output strings.
sep.join(iterable)','.join(['Alex', 'Admin', '100k'])'Alex,Admin,100k'Time Complexity: O(N)
Common Mistakes: Passing non-string elements raises TypeError.
Related Methods: replace()
join() vs += loop concatenation: join() allocates the entire string memory once (O(N) time); loop concatenation repeatedly allocates new memory blocks (O(N^2) time).
Replace all occurrences of a substring with another.
Used In: Text cleaning, normalizing date separators in ETL.
str.replace(old, new, count=-1)'2026/07/12'.replace('/', '-')'2026-07-12'Time Complexity: O(N)
Related Methods: translate()
Remove leading and trailing whitespace (or specific characters).
Used In: File processing, input cleaning.
str.strip(chars=None)' dirty log line\n '.strip()'dirty log line'Time Complexity: O(N)
Related Methods: lstrip(), rstrip()
strip() removes from both ends; lstrip() removes from the left; rstrip() removes from the right.
Remove leading whitespace (or specific characters) from the left side.
Used In: Log parsing, metadata prefix removal.
str.lstrip(chars=None)'***error_log'.lstrip('*')'error_log'Time Complexity: O(N)
Related Methods: strip(), rstrip()
Remove trailing whitespace (or specific characters) from the right side.
Used In: CSV cleaning, text cleanup.
str.rstrip(chars=None)'data_row,\n'.rstrip(',\n')'data_row'Time Complexity: O(N)
Related Methods: strip(), lstrip()
Convert all characters in the string to lowercase.
Used In: Standardizing raw text fields, data cleansing.
str.lower()'DataPlay'.lower()'dataplay'Time Complexity: O(N)
Related Methods: upper(), casefold()
Convert all characters in the string to uppercase.
Used In: Code normalization, flag comparisons.
str.upper()'status_ok'.upper()'STATUS_OK'Time Complexity: O(N)
Related Methods: lower()
Capitalize only the first character of the string.
Used In: Formatting names, UI titles.
str.capitalize()'alex smith'.capitalize()'Alex smith'Time Complexity: O(N)
Related Methods: title()
Capitalize the first character of every word in the string.
Used In: Reporting headers, names standardization.
str.title()'data engineer'.title()'Data Engineer'Time Complexity: O(N)
Related Methods: capitalize()
Check if string starts with specified prefix.
Used In: Routing raw log lines, error detection.
str.startswith(prefix, start=0, end=len(str))'ERR: Disk full'.startswith('ERR:')TrueTime Complexity: O(K) where K is prefix length
Related Methods: endswith()
Check if string ends with specified suffix.
Used In: Filtering directory files by extension.
str.endswith(suffix, start=0, end=len(str))'data.parquet'.endswith(('.parquet', '.pq'))TrueTime Complexity: O(K) where K is suffix length
Related Methods: startswith()
Find the first index of a substring, returning -1 if not found.
Used In: Locating token offsets, quick validations.
str.find(sub, start=0, end=len(str))'error log'.find('log')6Time Complexity: O(N)
Related Methods: index()
find() vs index(): find() returns -1 if the substring is missing; index() raises a ValueError.
Count occurrences of a substring in the string.
Used In: Log statistics, text aggregation.
str.count(sub, start=0, end=len(str))'OOM, OOM, Warning'.count('OOM')2Time Complexity: O(N)
Related Methods: find()
Format string values using curly brace placeholders.
Used In: Creating dynamic log lines, query templating.
str.format(*args, **kwargs)'{usr} logged {act}'.format(usr='Alex', act='IN')'Alex logged IN'Time Complexity: O(N)