str() String constructor
Last updated: 2026-09-22
str() String constructor
str() converts an object to str; with no args returns ''.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
str(object='')
str(object=b'', encoding='utf-8', errors='strict')
⚙️ Parameters
| object Optional | The object to convert; omitted returns ''.(Default:'') |
|---|---|
| encoding Optional | Encoding used when decoding bytes.(Default:'utf-8') |
| errors Optional | Error handling for decoding (strict/ignore/replace, etc.).(Default:'strict') |
Returns:The string form of the object; bytes are decoded using the given encoding.
💥 Raises
UnicodeDecodeError— Raised when bytes cannot be decoded with the given encoding.TypeError— Raised when encoding is given with a non-bytes object.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
42
[1, 2]
中
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat do str(42) and str([1, 2]) return?
AThey return '42' and '[1, 2]' — str() converts any object into a readable string.
QWhat does str() return with no arguments?
AIt returns an empty string ''.
QWhat does str(b'\xe4\xb8\xad', encoding='utf-8') return?
AIt returns '中'. A bytes object must be given an encoding to decode into a string; without encoding you only get a literal form like "b'\\xe4\\xb8\\xad'".
QWhat is the difference between str() and repr()?
Astr() is for humans (used by print by default), while repr() is for developers and aims to be reconstructible; e.g. str('a') is a while repr('a') is "'a'"