Home / Python / Reference

Python reference: built-in functions and methods

Every Python built-in function and every string, list, tuple, dictionary and set method you will use, explained in plain English. Each page has the syntax, the parameters, what it returns, a common mistake to avoid, and an example you can run and edit, with its real output.

New to Python? Start with the free Python course; this reference is for looking things up as you go.

Built-in functions

  • print() Show values on the screen (or write them to a file).
  • input() Ask the user to type a line, and return it as a string.
  • len() Return the number of items in a string, list, tuple, dictionary, set or range.
  • type() Return the type (class) of a value.
  • isinstance() Check whether a value is of a given type (or one of several types).
  • int() Convert a value to a whole number.
  • float() Convert a value to a number with a decimal point.
  • str() Convert a value to its text form.
  • bool() Convert a value to True or False.
  • list() Make a new list, empty or from any iterable.
  • tuple() Make a new tuple (an unchangeable sequence).
  • dict() Make a new dictionary.
  • set() Make a new set: an unordered collection of unique items.
  • range() A sequence of whole numbers, usually for loops.
  • enumerate() Loop over items together with their position.
  • zip() Walk several sequences side by side.
  • sum() Add up all the numbers in an iterable.
  • max() Return the largest item.
  • min() Return the smallest item.
  • sorted() Return a new sorted list from any iterable.
  • reversed() Loop over a sequence backwards.
  • round() Round a number to a given number of decimal places.
  • abs() Return the absolute value: the distance from zero, always positive.
  • pow() Raise a number to a power (the same as **).
  • divmod() Divide and return both the whole-number result and the remainder.
  • any() True if at least one item is truthy.
  • all() True if every item is truthy.
  • map() Apply a function to every item.
  • filter() Keep only the items for which a function returns True.
  • open() Open a file for reading or writing.
  • repr() Return a precise text form of a value, as you would type it in code.
  • format() Format a value using a format specification.
  • chr() Return the character for a Unicode code number.
  • ord() Return the Unicode code number of a character.
  • dir() List the names (attributes and methods) an object has.
  • help() Show the documentation for a function, class or module.
  • id() Return a number that identifies an object in memory.
  • iter() Get an iterator: an object that gives items one at a time.
  • next() Take the next item from an iterator.
  • callable() Check whether something can be called like a function.
  • getattr() Get an attribute by its name as a string.
  • hasattr() Check whether an object has an attribute.
  • setattr() Set an attribute by its name as a string.
  • bin() Write a whole number in binary, as a string starting with 0b.
  • hex() Write a whole number in hexadecimal (base 16), as a string starting with 0x.
  • frozenset() Make a set that cannot be changed.

String methods

  • str.split() Cut a string into a list of pieces.
  • str.join() Glue a list of strings together, with a separator between them.
  • str.strip() Remove spaces (or other characters) from both ends.
  • str.lstrip() Remove spaces (or other characters) from the left end only.
  • str.rstrip() Remove spaces (or other characters) from the right end only.
  • str.lower() Return the string in lower case.
  • str.upper() Return the string in upper case.
  • str.title() Capitalise the first letter of every word.
  • str.capitalize() Capitalise the first character and lower-case the rest.
  • str.casefold() A stronger lower(), for comparing text in any language.
  • str.swapcase() Swap upper and lower case.
  • str.replace() Replace every occurrence of some text with other text.
  • str.find() Return the position of the first occurrence of some text, or -1.
  • str.rfind() Return the position of the last occurrence, or -1.
  • str.index() Like find, but raises ValueError if the text is missing.
  • str.count() Count how many times some text appears (without overlapping).
  • str.startswith() Check whether a string starts with some text.
  • str.endswith() Check whether a string ends with some text.
  • str.removeprefix() Remove some text from the start, if it is there.
  • str.removesuffix() Remove some text from the end, if it is there.
  • str.isdigit() Check whether every character is a digit (and there is at least one).
  • str.isalpha() Check whether every character is a letter.
  • str.isalnum() Check whether every character is a letter or a digit.
  • str.isspace() Check whether every character is whitespace (spaces, tabs, new lines).
  • str.isupper() Check whether all letters are upper case.
  • str.zfill() Pad a number-string with zeros on the left to a given width.
  • str.center() Centre a string in a field of a given width.
  • str.ljust() Pad a string on the right so it is left-aligned in a fixed width.
  • str.rjust() Pad a string on the left so it is right-aligned in a fixed width.
  • str.partition() Split at the first separator into exactly three parts: before, separator, after.
  • str.rsplit() Like split, but counts the cuts from the right.
  • str.splitlines() Split text into a list of lines.
  • str.format() Insert values into the {} placeholders of a string.
  • str.encode() Turn text into bytes.
  • str.translate() Replace or delete many single characters at once.

List methods

Tuple methods

Dictionary methods

Set methods