Python str() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

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")
CallWhat 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

ParameterDescription
objectOptional. The value to convert. Default is the empty string.
encodingOptional. Codec name used to decode a bytes-like object, such as "utf-8".
errorsOptional. How to handle decoding errors: "strict" (default), "replace", "ignore", ...

Return Value

Return ValueDescription
strThe 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}")

How It Works

  1. Everything converts. Numbers, booleans, None, and containers all have a text form.
  2. Type on the left. type(item).__name__ is right-aligned in 8 characters to line up the arrows.
  3. Quotes show it is text. !r prints each result's repr, proving str() returned a string.
  4. Containers use item reprs. str({"a": 1}) keeps the inner quotes, so its repr switches to double quotes.

Example 2 — Fix "can only concatenate str" Errors

See why + fails between text and numbers, and convert numbers before joining them.

python
age = 30
try:
    message = "Age: " + age
except TypeError:
    message = "Age: " + str(age)
print(message)

numbers = [3, 1, 4]
print(", ".join(str(n) for n in numbers))
print(str(12.50), str(-0.0), str(10 ** 20))

How It Works

  1. No automatic conversion. "Age: " + 30 raises TypeError; Python does not guess that you want text.
  2. Convert explicitly. str(age) produces "30", and the concatenation works.
  3. join needs strings. str.join() requires text items, so each number goes through str() first.
  4. Number text. 12.50 prints as 12.5, negative zero keeps its sign, and big ints print every digit.

Example 3 — __str__ for Users and Decoding Bytes

Give a class separate user and developer text, then decode UTF-8 bytes with an encoding.

python
class Temperature:
    def __init__(self, celsius):
        self.celsius = celsius

    def __str__(self):
        return f"{self.celsius} C"

    def __repr__(self):
        return f"Temperature({self.celsius})"


t = Temperature(21.5)
print(str(t))
print(t)
print([t])

raw = b"caf\xc3\xa9"
print(ascii(str(raw, "utf-8")))
print(str(raw))

How It Works

  1. __str__ for people. str(t) and print(t) both use __str__ and show 21.5 C.
  2. __repr__ inside lists. A list prints its items with repr(), so [t] shows Temperature(21.5).
  3. Decode with an encoding. str(raw, "utf-8") turns the two bytes C3 A9 into the single character é.
  4. No encoding, no decoding. str(raw) just returns the bytes repr, which is rarely what you want.

Frequently Asked Questions

Call str(42) to get "42". In messages, an f-string such as f"Total: {total}" converts automatically.
str() gives readable text for users; repr() gives an unambiguous form for developers. str("hi") is hi, repr("hi") is 'hi' with quotes.
Decode them: str(data, "utf-8") or data.decode("utf-8"). str(data) without an encoding returns the repr like b'...'.
Python does not mix text and numbers in +. Convert first with str(30), or use f"Age: {30}".
It defines the text that str() and print() show for your objects. If it is missing, Python uses __repr__ instead.

Next: sum()

Add up the numbers in an iterable.

sum() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful