builtins – builtin functions and exceptions
All builtin functions and exceptions are described here. They are also
available via builtins module.
Functions and types
- abs()
- all()
- any()
- bin()
- class bool
- class bytes
See CPython documentation:
bytes.- decode(encoding='utf-8', errors='strict')
Decode the bytes object to a string using the specified encoding.
MicroPython supports the following encodings:
'utf-8'or'utf8'- UTF-8 encoding (default)'ascii'- ASCII encoding (subset of UTF-8)
The errors parameter controls how decoding errors are handled:
'strict'- Raise aUnicodeErroron invalid UTF-8 (default)'ignore'- Skip invalid bytes (requiresMICROPY_PY_BUILTINS_BYTES_DECODE_ERRORS)'replace'- Replace invalid bytes with U+FFFD ‘�’ (requiresMICROPY_PY_BUILTINS_BYTES_DECODE_ERRORS)
Note
Error handler support depends on build configuration. On constrained systems, only
'strict'mode may be available.Example:
>>> b'\xc2\xa9 2024'.decode('utf-8') # © symbol '© 2024' >>> b'hello\xffworld'.decode('utf-8', 'ignore') # Skip invalid bytes 'helloworld'
Raises
LookupErrorif the encoding is not supported, orUnicodeErrorif the data contains invalid UTF-8 anderrors='strict'.
- callable()
- chr()
- classmethod()
- compile()
- class complex
- delattr(obj, name)
The argument name should be a string, and this function deletes the named attribute from the object given by obj.
- class dict
- dir()
- divmod()
- enumerate()
- eval()
- exec()
- filter()
- class float
- class frozenset
- getattr()
- globals()
- hasattr()
- hash()
- hex()
- id()
- input()
- class int
- isinstance()
- issubclass()
- iter()
- len()
- class list
- locals()
- map()
- max()
- class memoryview
See CPython documentation:
memoryview.
- min()
- next()
- class object
- oct()
- open()
- ord()
- pow()
- print()
- property()
- range()
- repr()
- reversed()
- round()
- class set
- setattr()
- class slice
The slice builtin is the type that slice objects have.
- sorted()
- staticmethod()
- class str
- encode(encoding='utf-8')
Encode the string to bytes using the specified encoding.
MicroPython supports the following encodings:
'utf-8'or'utf8'- UTF-8 encoding (default)'ascii'- ASCII encoding (subset of UTF-8)
Example:
>>> '© 2024'.encode('utf-8') # Copyright symbol b'\xc2\xa9 2024'
Raises
LookupErrorif the encoding is not supported.
- center(width)
Return a centered string of length width. Padding is done using spaces.
When Unicode support is enabled (
MICROPY_PY_BUILTINS_STR_UNICODE), this method counts Unicode characters rather than bytes, ensuring proper alignment for multi-byte UTF-8 characters.Example:
>>> 'café'.center(10) # é is 2 bytes in UTF-8 ' café '
- sum()
- super()
- class tuple
- type()
- zip()
Exceptions
- exception AssertionError
- exception AttributeError
- exception Exception
- exception ImportError
- exception IndexError
- exception KeyboardInterrupt
See CPython documentation:
KeyboardInterrupt.See also in the context of Soft Bricking (failure to boot).
- exception KeyError
- exception MemoryError
- exception NameError
- exception NotImplementedError
- exception OSError
- exception RuntimeError
- exception StopIteration
- exception SyntaxError
- exception SystemExit
See CPython documentation:
SystemExit.On non-embedded ports (i.e. Windows and Unix), an unhandled
SystemExitexits the MicroPython process in a similar way to CPython.On embedded ports, an unhandled
SystemExitcurrently causes a Soft Reset of MicroPython.
- exception ValueError
- exception ZeroDivisionError