Code Reference

String Methods

Python · Methods reference

Python · Methods reference


📋 Overview

All standard str instance methods (Python 3). Strings are immutable — methods return new strings.

🔧 Methods

Search & test

MethodReturnsDescription
find(sub[, start[, end]])intFirst index or -1
rfind(sub[, start[, end]])intLast index or -1
index(sub[, start[, end]])intLike find but ValueError if missing
rindex(sub[, start[, end]])intLike rfind but ValueError if missing
count(sub[, start[, end]])intNon-overlapping occurrences
startswith(prefix[, start[, end]])boolPrefix test (tuple of prefixes OK)
endswith(suffix[, start[, end]])boolSuffix test
isalpha() / isdigit() / …boolCharacter class tests
isalnum() / isascii() / isdecimal()boolMore classification
isidentifier() / islower() / isupper()boolIdentifier / case checks
isnumeric() / isprintable() / isspace()boolNumeric / printable / whitespace
istitle()boolTitle-case words

Transform & format

MethodDescription
lower() / upper() / casefold()Case conversion (casefold for Unicode)
capitalize() / title() / swapcase()Capitalization variants
strip([chars]) / lstrip / rstripTrim whitespace or chars
removeprefix(p) / removesuffix(s)Remove prefix/suffix if present (3.9+)
replace(old, new[, count])Substring replace
translate(table)Character mapping via str.maketrans
maketrans(x, y[, z])Static: build translation table
format(*args, **kwargs)Format string with placeholders
format_map(mapping)Format using mapping only
encode(encoding=…, errors=…)Bytes encoding (utf-8 default)
split(sep=None, maxsplit=-1)Split into list
rsplit(sep=None, maxsplit=-1)Split from right
splitlines([keepends])Split on line boundaries
join(iterable)Join strings with self as separator
partition(sep) / rpartition(sep)Split into (head, sep, tail)
center(width[, fillchar])Center pad
ljust(width[, fillchar]) / rjustLeft/right pad
zfill(width)Zero-fill numeric strings
expandtabs([tabsize])Tabs to spaces

💡 Examples

See parent topic notes for runnable snippets; this page is the complete method index.

⚠️ Pitfalls

  • Mutating methods return None in Python — do not chain sort() / reverse() expecting a new list.
  • Default JS sort() compares strings — pass (a,b) => a-b for numbers.
  • SQL function names differ by dialect — verify Postgres vs MySQL docs.
  • Django QuerySet.update() skips save() signals and auto auto_now fields on models.

On this page