In Python, str(obj) returns a readable text version of any object: str(42) is "42", str(None) is "None", and str() with no argument is the empty string. print() uses str() on every value it shows.
Pass a bytes object together with an encoding (and optionally errors) to decode it into text: str(b"hi", "utf-8") is "hi". Strings are covered in depth on the Python Strings page and the String Methods index.
str is a built-in type (a class) that you call like a function: each call creates a new str object. Like every built-in, it lives in the builtins module and is always available without an import.
Syntax
Convert an object, or decode bytes with an encoding.
python
text = str(obj)
text = str(raw_bytes, encoding="utf-8", errors="strict")
Call
What it does
str(3.5)
Returns "3.5".
str([1, 2])
Returns "[1, 2]".
str(b"hi", "utf-8")
Returns "hi" (decoding).
str(b"hi")
Returns "b'hi'": without an encoding you get the repr.
Python never converts numbers to text automatically in +: write "Age: " + str(age), or better, an f-string.
Library declarations
python
str(object="")
str(object=b"", encoding="utf-8", errors="strict")
# class str: str is a built-in type
Parameters
Parameter
Description
object
Optional. The value to convert. Default is the empty string.
encoding
Optional. Codec name used to decode a bytes-like object, such as "utf-8".
errors
Optional. How to handle decoding errors: "strict" (default), "replace", "ignore", ...
Return Value
Return Value
Description
str
The object's __str__() result, or the decoded text.
Python Examples
Example 1 — Convert Different Types to Text
Turn seven kinds of values into strings and print each with its type name and repr.
python
items = [42, 3.5, True, None, [1, 2], (3,), {"a": 1}]
for item in items:
text = str(item)
print(f"{type(item).__name__:>8} -> {text!r}")