Built-in Functions and Types
str()
Create Unicode text. With no encoding or errors argument, use the object string representation protocol; bytes are represented as a bytes literal rather than decoded. Supplying encoding or errors selects decoding of a bytes-like object. The default codec in decoding mode is UTF-8, and strict errors reject malformed bytes. Strings are immutable. Invalid decoding input raises TypeError; invalid byte sequences can raise UnicodeDecodeError.
Formal Syntax & Structure
python syntaxpython
str(object='')str(object, encoding='utf-8', errors='strict')Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| object | object or bytes-like | Value to represent, or bytes-like input for decoding; defaults to empty text. |
| encoding | str | Codec name for decoding; default UTF-8. |
| errors | str | Codec error policy; default strict. |
Return Value
str
Unicode string; representation and decoding are distinct operations.
Working Example
Example Snippetpython
raw = b"caf\xc3\xa9"print(str(raw))print(str(raw, "utf-8"))print(str(42))