PYTHON Reference Guide
Revision Time: 4 mins

String Methods Reference

Print formatting, cleanups, splits, and substring substitutions.

split
Return: List[str]

Split string into list by separator.

Used In: CSV parsing, log parsing, tokenization.

Syntax signature:str.split(sep=None, maxsplit=-1)
Code snippet:
python
'name,role,salary'.split(',')
Expected Output:['name', 'role', 'salary']

Time Complexity: O(N)

Common Mistakes: Calling split(',') on an empty string yields [''] rather than [].

Related Methods: partition(), splitlines()

Comparison:

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).

Remember: Defaults to whitespace split if no sep parameter is specified.
join
Return: str

Concatenate list items with a separator.

Used In: Creating CSV rows, constructing output strings.

Syntax signature:sep.join(iterable)
Code snippet:
python
','.join(['Alex', 'Admin', '100k'])
Expected Output:'Alex,Admin,100k'

Time Complexity: O(N)

Common Mistakes: Passing non-string elements raises TypeError.

Related Methods: replace()

Comparison:

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).

Remember: Much faster than appending strings in a loop (+=) which causes O(N^2) copying.
replace
Return: str

Replace all occurrences of a substring with another.

Used In: Text cleaning, normalizing date separators in ETL.

Syntax signature:str.replace(old, new, count=-1)
Code snippet:
python
'2026/07/12'.replace('/', '-')
Expected Output:'2026-07-12'

Time Complexity: O(N)

Related Methods: translate()

Remember: Set count to limit the number of replacements.
strip
Return: str

Remove leading and trailing whitespace (or specific characters).

Used In: File processing, input cleaning.

Syntax signature:str.strip(chars=None)
Code snippet:
python
'  dirty log line\n '.strip()
Expected Output:'dirty log line'

Time Complexity: O(N)

Related Methods: lstrip(), rstrip()

Comparison:

strip() removes from both ends; lstrip() removes from the left; rstrip() removes from the right.

Remember: Very useful for stripping whitespace and newlines from raw text files.
lstrip
Return: str

Remove leading whitespace (or specific characters) from the left side.

Used In: Log parsing, metadata prefix removal.

Syntax signature:str.lstrip(chars=None)
Code snippet:
python
'***error_log'.lstrip('*')
Expected Output:'error_log'

Time Complexity: O(N)

Related Methods: strip(), rstrip()

Remember: Only targets characters starting at the left boundary.
rstrip
Return: str

Remove trailing whitespace (or specific characters) from the right side.

Used In: CSV cleaning, text cleanup.

Syntax signature:str.rstrip(chars=None)
Code snippet:
python
'data_row,\n'.rstrip(',\n')
Expected Output:'data_row'

Time Complexity: O(N)

Related Methods: strip(), lstrip()

Remember: Commonly used to strip trailing newlines and commas from file lines.
lower
Return: str

Convert all characters in the string to lowercase.

Used In: Standardizing raw text fields, data cleansing.

Syntax signature:str.lower()
Code snippet:
python
'DataPlay'.lower()
Expected Output:'dataplay'

Time Complexity: O(N)

Related Methods: upper(), casefold()

Remember: Excellent for case-insensitive comparisons.
upper
Return: str

Convert all characters in the string to uppercase.

Used In: Code normalization, flag comparisons.

Syntax signature:str.upper()
Code snippet:
python
'status_ok'.upper()
Expected Output:'STATUS_OK'

Time Complexity: O(N)

Related Methods: lower()

Remember: Commonly used to standardize status flags in pipelines.
capitalize
Return: str

Capitalize only the first character of the string.

Used In: Formatting names, UI titles.

Syntax signature:str.capitalize()
Code snippet:
python
'alex smith'.capitalize()
Expected Output:'Alex smith'

Time Complexity: O(N)

Related Methods: title()

Remember: Only the first letter of the entire string becomes capital; the rest becomes lowercase.
title
Return: str

Capitalize the first character of every word in the string.

Used In: Reporting headers, names standardization.

Syntax signature:str.title()
Code snippet:
python
'data engineer'.title()
Expected Output:'Data Engineer'

Time Complexity: O(N)

Related Methods: capitalize()

Remember: Be careful: apostrophes capitalize the next letter (e.g. they's -> They'S).
startswith
Return: bool

Check if string starts with specified prefix.

Used In: Routing raw log lines, error detection.

Syntax signature:str.startswith(prefix, start=0, end=len(str))
Code snippet:
python
'ERR: Disk full'.startswith('ERR:')
Expected Output:True

Time Complexity: O(K) where K is prefix length

Related Methods: endswith()

Remember: You can pass a tuple of prefixes to check multiple alternatives.
endswith
Return: bool

Check if string ends with specified suffix.

Used In: Filtering directory files by extension.

Syntax signature:str.endswith(suffix, start=0, end=len(str))
Code snippet:
python
'data.parquet'.endswith(('.parquet', '.pq'))
Expected Output:True

Time Complexity: O(K) where K is suffix length

Related Methods: startswith()

Remember: Pass a tuple of suffixes to support multiple extensions.
find
Return: int

Find the first index of a substring, returning -1 if not found.

Used In: Locating token offsets, quick validations.

Syntax signature:str.find(sub, start=0, end=len(str))
Code snippet:
python
'error log'.find('log')
Expected Output:6

Time Complexity: O(N)

Related Methods: index()

Comparison:

find() vs index(): find() returns -1 if the substring is missing; index() raises a ValueError.

Remember: Does not throw an exception (unlike index()), making it safer for checking existence.
count
Return: int

Count occurrences of a substring in the string.

Used In: Log statistics, text aggregation.

Syntax signature:str.count(sub, start=0, end=len(str))
Code snippet:
python
'OOM, OOM, Warning'.count('OOM')
Expected Output:2

Time Complexity: O(N)

Related Methods: find()

Remember: Counts only non-overlapping occurrences.
format
Return: str

Format string values using curly brace placeholders.

Used In: Creating dynamic log lines, query templating.

Syntax signature:str.format(*args, **kwargs)
Code snippet:
python
'{usr} logged {act}'.format(usr='Alex', act='IN')
Expected Output:'Alex logged IN'

Time Complexity: O(N)

Remember: For newer codebases, f-strings (f'{var}') are preferred for performance.