str
Convert an object to a string.
Python 3.13
str(object='')
str(object=b'', encoding='utf-8', errors='strict')- =default value
- object
- The object to represent as a string.AnyOptional
- encoding
- The encoding if object is bytes, such as 'utf-8'; with it, the bytes are decoded into text.strOptional
- errors
- What to do with bytes that cannot be decoded; 'strict' by default, which raises an error.strOptional'strict''ignore''replace''backslashreplace''surrogateescape'
Return value
A string with the text representation of the object; an empty string without arguments.
Examples
String from a number
Python 3.13
text = str(123)
print(text + '4')
print(len(text))1234 3
Bytes with and without an encoding
Python 3.13
print(str(b'hi'))
print(str(b'hi', 'utf-8'))b'hi' hi
Details
For strings, str returns the text as is, while repr adds quotes: str("hi") gives hi, and repr("hi") gives 'hi'. Bytes without encoding turn into their b'...' notation, and with encoding into decoded text; bytes.decode does the same. With errors='ignore', undecodable bytes are skipped, with 'replace' they become �, and with 'backslashreplace' they become escapes such as \xff.
