Python functions

Built-in functions, string and list methods, dictionary and set operations. Every entry with a LIVE pill has a live in-browser demo.

Methods on the built-in string type. Immutable — every method returns a new string.

LIVE
str.capitalize
capitalize()
Return a copy with the first character titlecased and the rest lowercased.
LIVE
str.endswith
endswith(suffix, start=0, end=len(s))
Test whether the string ends with a suffix.
LIVE
str.find
find(sub, start=0, end=len(s))
Return the lowest index of a substring, or -1 if absent.
LIVE
str.isdigit
isdigit()
Test whether all characters are digits and the string is non-empty.
LIVE
str.join
join(iterable)
Concatenate an iterable of strings with this string as the separator.
LIVE
str.lower
lower()
Return a copy of the string with all cased characters lowercased.
LIVE
str.replace
replace(old, new, count=-1)
Return a copy with all occurrences of old replaced by new.
LIVE
str.rstrip
rstrip(chars=None)
Return a copy with trailing characters removed.
LIVE
str.split
split(sep=None, maxsplit=-1)
Break a string into a list of substrings on a delimiter.
LIVE
str.startswith
startswith(prefix, start=0, end=len(s))
Test whether the string begins with a prefix.
LIVE
str.casefold
casefold()
Aggressive lowercase for case-insensitive comparison — stronger than lower().
LIVE
str.center
center(width, fillchar=" ")
Center within a given width, padding both sides.
LIVE
str.count
count(sub, start=0, end=len(s))
Count non-overlapping occurrences of a substring.
LIVE
str.encode
encode(encoding="utf-8", errors="strict")
Convert a string to bytes using the given encoding — the string-to-bytes boundary.
LIVE
str.expandtabs
expandtabs(tabsize=8)
Replace tabs with spaces to align at the next tab stop — not with a fixed number of spaces.
LIVE
str.format
format(*args, **kwargs)
Fill `{}` placeholders — positional, numbered, or named — with the format spec after the colon.
LIVE
str.format_map
format_map(mapping)
Like format(**mapping), but without copying the mapping first.
LIVE
str.index
index(sub[, start[, end]])
Position of the first occurrence — raises ValueError instead of returning -1.
LIVE
str.isalnum
isalnum()
True if every character is a letter or digit and the string is non-empty.
LIVE
str.isalpha
isalpha()
True if every character is alphabetic and the string is non-empty.
LIVE
str.isascii
isascii()
True if every character is in the ASCII range (0..127) — empty string also True.
LIVE
str.isdecimal
isdecimal()
The narrowest numeric check — decimal digits only, no superscripts, no fractions.
LIVE
str.isidentifier
isidentifier()
True if the string could be a valid Python identifier — reserved keywords also pass.
LIVE
str.islower
islower()
True if every cased character is lowercase and the string has at least one cased character.
LIVE
str.isnumeric
isnumeric()
True if every character is numeric — including Roman numerals and fractions.
LIVE
str.isprintable
isprintable()
True if every character is printable and the string has no control characters — empty is True.
LIVE
str.isspace
isspace()
True if every character is whitespace and the string is non-empty.
LIVE
str.istitle
istitle()
True if the string is in strict title case — first letter of each word uppercase, rest lowercase.
LIVE
str.isupper
isupper()
True if every cased character is uppercase and the string has at least one cased character.
LIVE
str.ljust
ljust(width, fillchar=" ")
Left-align within a given width — pad on the right.
LIVE
str.lstrip
lstrip(chars=None)
Strip leading whitespace — or leading characters from a given set.
LIVE
str.maketrans
maketrans(x[, y[, z]])
Build a translation table for str.translate — three call shapes.
LIVE
str.partition
partition(sep)
Split at the first occurrence of sep — always into three pieces.
LIVE
str.removeprefix
removeprefix(prefix)
Remove an exact leading substring — safer than lstrip for prefix stripping.
LIVE
str.removesuffix
removesuffix(suffix)
Remove an exact trailing substring — safer than rstrip for suffix stripping.
LIVE
str.rfind
rfind(sub, start=0, end=len(s))
Return the highest index of a substring, or -1 if absent.
LIVE
str.rindex
rindex(sub[, start[, end]])
Position of the LAST occurrence — raises ValueError instead of returning -1.
LIVE
str.rjust
rjust(width, fillchar=" ")
Right-align within a given width — pad on the left.
LIVE
str.rpartition
rpartition(sep)
Split at the LAST occurrence of sep — always into three pieces.
LIVE
str.rsplit
rsplit(sep=None, maxsplit=-1)
Split from the right — behaves like split unless maxsplit is set.
LIVE
str.splitlines
splitlines(keepends=False)
Split at universal newlines — no trailing empty string.
LIVE
str.swapcase
swapcase()
Flip case per character — uppercase becomes lowercase and vice versa.
LIVE
str.translate
translate(table)
Apply a character-mapping table — replace, delete, or transform in one pass.
LIVE
str.strip
strip(chars=None)
Return a copy with leading and trailing characters removed.
LIVE
str.title
title()
Return a titlecased copy — every word starts uppercase.
LIVE
str.upper
upper()
Return a copy of the string with all cased characters uppercased.
LIVE
str.zfill
zfill(width)
Left-pad with zeros to a given width, sign-aware.