Python String Methods Reference

All Python str methods with signature, return type and what they do

Searchable reference for every Python str method — case, search, split, strip, replace, encode, and format — with each method's signature, return type, and behavior notes.

Are Python strings mutable?

No. Python str objects are immutable, so every method that appears to change a string actually returns a new string and leaves the original unchanged. For example s.upper() does not modify s; you must assign the result, like s = s.upper(). This is why methods such as replace and strip return a value rather than acting in place.

Python’s str type is immutable, so every method here returns a new string rather than changing the original. The methods cover case conversion, searching, splitting and joining, trimming, testing, and encoding. This is a searchable offline reference.

How it works

Each method lists its signature and return type. Because strings are immutable, a call like s.upper() produces a new string — you must assign it (s = s.upper()) to keep the result. The groups are:

  • Caseupper, lower, title, capitalize, casefold, swapcase.
  • Searchfind, index, count, startswith, endswith. find returns -1 on failure; index raises ValueError.
  • Split / joinsplit, rsplit, splitlines, partition, join.
  • Trim / padstrip, lstrip, rstrip, removeprefix, removesuffix, center, ljust, rjust, zfill.
  • Testisalpha, isdigit, isalnum, isspace, isidentifier, etc.
  • Transformreplace, translate, format, format_map, encode.

Example

"  Hello, World  ".strip().lower().replace(",", "")
# -> 'hello world'

"file.tar.gz".removesuffix(".gz")  # -> 'file.tar'

Note strip('.py') would remove any trailing ., p, or y character — use removesuffix('.py') to drop the exact substring once.

Notes

  • strip/lstrip/rstrip take a set of characters, not a prefix — use removeprefix/removesuffix (Python 3.9+) for exact substrings.
  • str.join is called on the separator: ", ".join(items).
  • This reference covers the standard str methods. For exact version availability consult the official Python docs.