Last updated: Apr 8, 2024
Reading time·3 min
The Python "TypeError: __str__ returned non-string (type NoneType)" occurs
when we forget to return a value from the __str__()
method.
To solve the error, make sure to return a string from the method.
Here is an example of how the error occurs.
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __str__(self): print(self.name) # 👈️ Forgot to return a value emp = Employee('Bobby Hadz', 100) # ⛔️ TypeError: __str__ returned non-string (type NoneType) print(emp)
We forgot to return a value from the __str__()
method in the Employee
class.
return
statement to return a value from __str__()
To solve the error, use a return statement to return a string from the method.
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __str__(self): return self.name # 👈️ return a string emp = Employee('Bobby Hadz', 100) print(emp) # 👉️ Bobby Hadz
The return value of the __str__()
method must be a string object.
__str__()
method must return a value of type stringIf the value you are returning is not a string, use the str() class to convert it.
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __str__(self): return str(self.salary) # 👈️ Return a string emp = Employee('Bobby Hadz', 100) print(emp) # 👉️ 100
We used the str
class to convert the integer to a string.
This is necessary because returning a value of a different type from the
__str__
method is not allowed.
Here is an example that tries to return an integer from the method.
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __str__(self): # ⛔️ TypeError: __str__ returned non-string (type int) return self.salary emp = Employee('Bobby Hadz', 100) print(emp) # 👉️ 100
The code sample causes the following error.
Traceback (most recent call last): File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 12, in <module> print(emp) # 👉️ 100 ^^^^^^^^^^ TypeError: __str__ returned non-string (type int)
We tried to return an integer from the __str__
method, which caused the
TypeError
.
You can use a formatted string literal if you need to include expressions in a string.
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __str__(self): return f'Name of employee: {self.name}' emp = Employee('Alice', 100) print(emp) # 👉️ Name of employee: Alice
f
.Make sure to wrap expressions in curly braces - {expression}
.
You can also use the addition (+) operator to concatenate strings.
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __str__(self): return 'Salary: ' + str(self.salary) emp = Employee('Bobby Hadz', 100) print(emp) # 👉️ Salary: 100
Note that we had to use the str()
class to convert the salary
attribute to a
string.
This is necessary because the values on the left and right-hand sides of the addition (+) operator need to be of compatible types (e.g. both strings or both numbers).
The
__str__()
method is called by str(object)
and the built-in functions format()
and
print() and returns the informal string
representation of the object.
You can learn more about the related topics by checking out the following tutorials: