conversion
str()
Returns a str version of an object. With bytes/bytearray, decodes using encoding.
str(object='', encoding='utf-8', errors='strict') Parameters
| Parameter | Purpose |
|---|---|
| object | Object to stringify (via __str__ or __repr__) |
| encoding | Codec for decoding bytes-like objects |
| errors | 'strict', 'ignore', 'replace' error handler |
Examples
str(42) Returns '42'
str(b'hi', 'utf-8') Returns 'hi'
str([1, 2]) Returns '[1, 2]'
str(b'\xff', 'utf-8', errors='replace') Returns replacement char
Gotcha
str(b'bytes') without encoding returns the repr "b'bytes'" — pass encoding explicitly, or use .decode().
Related built-ins
int()
Constructs an integer from a number or string. When x is a string, base selects the numeric base (0 auto-detects from prefix).
float()
Constructs a floating-point number from a number or string. Accepts 'inf', '-inf', 'nan' (case-insensitive).
open()
Opens a file and returns a file object. Default mode 'r' is text read; use 'rb'/'wb' for binary.
dict()
Constructs a dictionary from keyword args, another mapping, or an iterable of key/value pairs. Insertion order is preserved (Python 3.7+).