Python String Methods
Every str method that matters — signature, real examples with expected output,
and the gotcha for each one. All Python strings are immutable — methods return new strings.
Search
str.find
Returns the lowest index in the string where substring sub is found within the slice [start:end]. Returns -1 if the substring is not found, unlike index() which raises ValueError.
str.rfind
Returns the highest index in the string where substring sub is found within [start:end]. Returns -1 if the substring is not found.
str.index
Like find(), but raises ValueError if the substring is not found. Use when the substring is expected to exist and its absence is an error.
str.count
Returns the number of non-overlapping occurrences of substring sub in [start:end]. Overlapping matches are not counted.
str.startswith
Returns True if the string starts with the given prefix, otherwise False. prefix may also be a tuple of strings to test against multiple candidates.
str.endswith
Returns True if the string ends with the given suffix, otherwise False. suffix may be a tuple of strings to test multiple candidates at once.
Transformation
str.replace
Returns a new string with all occurrences of substring old replaced by new. If count is given, only the first count occurrences are replaced.
str.upper
Returns a copy of the string with all cased characters converted to uppercase. Non-cased characters (digits, punctuation, whitespace) are unchanged.
str.lower
Returns a copy of the string with all cased characters converted to lowercase. Uses Unicode simple lowercase mappings.
str.title
Returns a titlecased copy where the first letter of each word is uppercase and the rest are lowercase. Word boundaries are defined by any run of non-letter characters, including apostrophes.
str.casefold
Returns a casefolded copy suitable for caseless matching. Stronger than lower(): it applies full Unicode case folding, e.g. German 'ß' becomes 'ss'.
Inspection (is*)
str.isdigit
Returns True if every character in the string is a digit and the string is non-empty. Includes Unicode digit characters like superscripts (²) but excludes signs, decimal points, and fractions like ½.
str.isalpha
Returns True if the string is non-empty and every character is an alphabetic Unicode letter. Digits, whitespace, and punctuation all return False.
str.isspace
Returns True if the string is non-empty and every character is whitespace. Whitespace includes spaces, tabs, newlines, and other Unicode whitespace characters.
Formatting & Padding
str.format
Substitutes replacement fields delimited by { } in the string with values from args and kwargs, using the Format Specification Mini-Language. In modern Python, f-strings are usually preferred for literals; str.format() remains ideal for templates loaded at runtime.
str.strip
Returns a copy of the string with leading and trailing whitespace removed. If chars is given, removes any leading/trailing characters that appear in the chars set (not a prefix/suffix — it is a character set).
str.lstrip
Returns a copy of the string with leading characters removed. Defaults to whitespace; a chars argument specifies a set of characters to strip from the left.
str.rstrip
Returns a copy of the string with trailing characters removed. Defaults to whitespace; a chars argument specifies a set of characters to strip from the right.
str.rjust
Returns the string right-justified in a field of the given width, padded on the left with fillchar. Returns the original string unchanged if it is already at least width characters long.
str.center
Returns the string centered in a field of the given width, padded on both sides with fillchar. When padding is uneven, the extra pad placement depends on the parity of width and margin — for the common odd-width case, it lands on the LEFT.
str.zfill
Returns a copy padded on the left with '0' digits to reach the given width. Unlike rjust('0'), it preserves a leading sign ('+' or '-') by placing the zeros after it.
Splitting & Joining
str.split
Returns a list of substrings split at each occurrence of sep. If sep is None (default), runs of any whitespace are the separator and leading/trailing whitespace is discarded; if sep is given, empty fields between adjacent separators are preserved.
str.splitlines
Splits the string at universal line boundaries (\n, \r, \r\n, and other Unicode line terminators). Unlike split('\n'), it does not produce a trailing empty string when the input ends with a newline.
str.partition
Splits the string at the first occurrence of sep and returns a 3-tuple (before, sep, after). If sep is not found, returns (original, '', '') — so unpacking is always safe.
str.join
Returns a string built by concatenating every string in the iterable, separated by the string on which join is called. Every element of the iterable must be a str, or TypeError is raised.