How to Resolve "TypeError: str returned non-string" in Python
The TypeError: __str__ returned non-string (type NoneType)
error (or a similar error with a different type like int
, list
, etc.) occurs in Python when the __str__()
method of a class does not return a string object. The __str__()
method is a special method that's used to define the "informal" or user-friendly string representation of an object. It's what's called when you use str(object)
, print(object)
, or format strings with an object.
This guide explains the cause of this error and demonstrates how to fix it.
Understanding the __str__()
Method
The __str__()
method is a special ("dunder" or "magic") method in Python classes. Its purpose is to provide a string representation of an object that is:
- Readable: Intended for end-users.
- Informal: A clear, concise description, not necessarily a complete representation.
When you call str(object)
, print(object)
, or use an object in an f-string, Python automatically calls the object's __str__()
method (if it exists) to get a string representation. If __str__()
is not defined, Python falls back to __repr__()
. If neither method is available, it will return something like <__main__.Employee object at 0x...>
.
Common Causes and Solutions
Forgetting to return
a Value**
The most common mistake is forgetting the return
statement in __str__()
:
class Employee():
def __init__(self, name, salary):
self.name = name
self.salary = salary
def __str__(self):
print(self.name) # ⛔️ ERROR: Prints to console, but RETURNS NONE
emp = Employee('Tom Nolan', 100)
print(emp) # ⛔️ TypeError: __str__ returned non-string (type NoneType)
-
Problem: The
__str__()
method prints the name, but it doesn't return anything. Functions that don't explicitly return a value implicitly returnNone
. -
Solution: Always
return
a string from__str__()
:class Employee():
def __init__(self, name, salary):
self.name = name
self.salary = salary
def __str__(self):
return self.name # ✅ Correct: Returns a string
Returning a Non-String Value
The __str__()
method must return a string. Returning anything else (an integer, a list, a dictionary, None
, etc.) will cause the TypeError
.
class Employee():
def __init__(self, name, salary):
self.name = name
self.salary = salary
def __str__(self):
return self.salary # ⛔️ ERROR: Returns an integer, not a string
emp = Employee('Tom Nolan', 100)
# print(emp) # TypeError: __str__ returned non-string (type int)
Solution: Convert the value to a string before returning it, typically using str()
or an f-string:
class Employee():
def __init__(self, name, salary):
self.name = name
self.salary = salary
def __str__(self):
return str(self.salary) # ✅ Correct: Returns a string
Using f-strings and String Concatenation in __str__()
Within __str__()
, you'll often want to create a formatted string representation of your object. F-strings are generally the most readable way to do this:
class Employee():
def __init__(self, name, salary):
self.name = name
self.salary = salary
def __str__(self):
return f'Name of employee: {self.name}' # Use f-strings for clarity
emp = Employee('Alice', 100)
print(emp) # Output: Name of employee: Alice
You can also use string concatenation, but this is less readable:
class Employee():
def __init__(self, name, salary):
self.name = name
self.salary = salary
def __str__(self):
return 'Salary: ' + str(self.salary) # Concatenate with string.
Conclusion
The TypeError: __str__ returned non-string
error is always caused by an incorrect return type from the __str__()
method.
- Ensure that your
__str__()
method always returns a string object. - Use
str()
to convert other types to strings if necessary, and prefer f-strings for creating formatted string representations of your objects.
By following these guidelines, you'll avoid this common error and create classes with clear and helpful string representations.