Python String Type Functions Cheat Sheet

This is a list of the string type functions summarized in https://docs.python.org/3/library/stdtypes.html.

Function Description
str.capitalize() Return string with first letter capitalized and rest lowercase.
str.casefold() Make string lowercase with additional special characters (e.g. German ß) changed.
str.center(width, {fillchar}) Center string in string with new width, with padded spaces or {fillchar} padding.
str.count(substring, {start, {end}}) Return number of substrings in string, over entire string or from {start} index, or from {start} to {end} indices.
str.encode(encoding=’utf-8′, errors=’strict’) Convert string to byte array with default (utf-8) or specified encoding. Second parameter refers to how errors should be handled, see here.
str.endswith(substring, {start, {end}}) Return True if string ends with substring. Optional index range can be specified.
str.expandtabs(tabsize=8) Replace tabs in string with number of spaces specified by tabsize parameter.
str.find(substring, {start, {end}}) Find substring in string, return index or -1. Optional index range can be specified.
str.format(*args, **kwargs) Print a formatted string. See here.
str.format_map(mapclass) This function can take a custom keyworded mapping class and use it to print formatted output, useful if some key parameters may not have corresponding values.
str.index(substring, {start, {end}} Same as “find” function, but raise ValueError if substring isn’t found, rather than returning -1.
str.isalnum() Return True if string only contains alphanumeric characters.
str.isalpha() Return True if string only contains alphabetic characters.
str.isascii() Return True if string contains only ASCII characters (the first 128 chars in Unicode encoding).
str.isdecimal() Return True if string only contains base-10 digits.
str.isdigit() Like isdecimal(), but include characters such as superscript digits.
str.isidentifier() Return True if string can be used as variable name.
str.islower() Return True if characters are lowercase and there is at least one alphabetic character.
str.isnumeric() Return True if characters are all digits and/or valid numeric Unicode characters.
str.isnumeric() Return False if string contains Unicode characters which are considered unprintable.
str.isspace() Return True if string contains only whitespace characters.
str.istitle() Return True if string is valid titlecase (uppercase letters don’t immediately follow lowercase letters, words begin with uppercase letters).
str.isupper() Return True if string only contains uppercase characters.
str.join(iterable) Return new string that has string representation of elements in the iterable data structure joined with string.
str.ljust(width, {padding}) Return left justified string with width {width} and padded spaces, or charater specifed by {padding}.
str.lower() Return string converted to lowercase.
str.lstrip({chars}) Return string with spaces or characters in {chars} removed from front of string.
static str.maketrans({x, {y, {z}) Make translation table for str.translate(table).
str.partition(sep) Return 3-tuple with string before separator, separator, and string after separator, or full string followed by two empty elements if separator doesn’t occur in string.
str.removeprefix(prefix) Return string with prefix removed, or original string if string doesn’t have prefix.
str.removesuffix(suffix) Return string with suffix removed, or original string if string doesn’t have suffix.
str.replace(old, new, {count}) Return string with old substrings replaced with new substring, throughout entire string or with {count} replacements maximum.
str.rfind(sub, {start, {end}}) Return index of rightmost match of substring in string, using entire string or from {start} to optional {end}.
str.rindex(sub, {start, {end}}) Same as rfind, but raise ValueError rather than returning -1 if substring doesn’t occur.
str.rjust(width, {padding}) Return right-justified string of width {width}, with padded spaces or character specified in {padding}.
str.rpartition(sep) Like str.partition(sep), but start searching for separator from end of string rather than beginning.
str.rsplit(sep=None, maxsplit=-1) Like str.split function, but start from end of string rather than beginning.
str.rstrip({chars}) Return string with trailing spaces or characters in {chars} removed.
str.split(sep=None, maxsplit=-1) Return list with string split at spaces or {sep}, split throughout string or a maximum of {maxsplit} times.
str.splitlines(keepends=False) Return list of strings split at newline chars, set {keepends} to True to keep the newline chars in split strings.
str.startswith(sub, {start, {end}}) Return True if string starts with substring, can also check string from {start} idx to optional {end} idx.
str.strip({chars}) Return string with leading and trailing spaces or characters specfied in {chars} stripped.
str.swapcase() Return string with lowercase chars converted to uppercase, and vice versa.
str.title() Return titlecase string, where words start with capital letters, doesn’t work properly with apostrophes though.
str.translate(table) Return string with characters converted to others using table construted with str.maketrans function.
str.upper() Return string with characters converted uppercase.
str.zfill(width) Return string with a width of {width}, padding beginning of string with zeros if original string isn’t wide enough.