str()
str(object='', encoding='utf-8', errors='strict') Returns:
str · Updated March 13, 2026 · Built-in Functions built-in conversion strings type-conversion
The str() function converts an object to its string representation. It is one of the most frequently used built-in functions in Python, invoked implicitly whenever you display objects with print() or concatenate values into strings. When called with no arguments, str() returns an empty string. If the object implements __str__(), that method is called; otherwise, __repr__() is used as a fallback.
Syntax
str(object='')
str(bytes_or_buffer, encoding='utf-8', errors='strict')
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
object | object | '' | The object to convert to a string. If omitted, returns an empty string. When not a bytes object, calls object.__str__() or falls back to repr(). |
encoding | str | 'utf-8' | The encoding to use when decoding bytes-like objects. Required if passing bytes. |
errors | str | 'strict' | Error handling scheme: 'strict' (raise UnicodeDecodeError), 'ignore' (skip invalid characters), or 'replace' (replace with replacement marker). |
Returns: A string representation of the object.
Examples
Basic type conversions
# Convert integers to strings
print(str(42))
# 42
# Convert floats
print(str(3.14159))
# 3.14159
# Convert lists
print(str([1, 2, 3]))
# [1, 2, 3]
# Convert None
print(str(None))
# None
Working with bytes and encoding
When converting bytes to strings, you must specify the encoding. This is where str() behaves like bytes.decode():
# Decode UTF-8 bytes
print(str(b'hello', 'utf-8'))
# hello
# Handle special characters
print(str(b'caf\xc3\xa9', 'utf-8'))
# café
# Error handling - ignore invalid bytes
print(str(b'hello\x80world', 'utf-8', 'ignore'))
# helloworld
# Error handling - replace with placeholder
print(str(b'hello\x80world', 'utf-8', 'replace'))
# hello�world
Custom objects with str
Classes can define __str__() to customize their string representation:
class User:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"User({self.name}, {self.age})"
user = User("Alice", 30)
print(str(user))
# User(Alice, 30)
Objects without __str__() fall back to repr():
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(3, 4)
print(str(p))
# <__main__.Point object at 0x...>
Common Patterns
String concatenation with str()
# Building strings from multiple values
name = "Alice"
age = 30
message = "Hello, " + name + ", you are " + str(age) + " years old."
print(message)
# Hello, Alice, you are 30 years old.
Converting user input
# Input always returns a string
user_input = input("Enter a number: ")
number = int(user_input) # Convert string to int
print(number * 2)
Safe string conversion in logging
# Convert any object to string for logging (never raises)
def log_value(value):
return str(value)
print(log_value([1, 2, 3]))
# [1, 2, 3]
print(log_value({"key": "value"}))
# {'key': 'value'}
Reading file content as strings
# Reading bytes from a file and decoding
with open("data.txt", "rb") as f:
content = f.read()
text = str(content, "utf-8")
Errors
TypeError with incorrect arguments
# Missing encoding when passing bytes
str(b"hello")
# TypeError: string argument without an encoding
# Too many positional arguments
str("hello", "utf-8")
# TypeError: str() argument 'errors' must be str, not None
UnicodeDecodeError with wrong encoding
# Invalid UTF-8 sequence with strict error handling
str(b"\xff\xfe", "utf-8", "strict")
# UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff...
See Also
- built-in::repr — returns a developer-friendly string representation (includes quotes for strings)
- str::format — formats values into styled strings with placeholders
- built-in::int — converts strings to integers