TypeError: __str__ returned non-string (type NoneType)

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# TypeError: __str__ returned non-string (type NoneType)

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.

typeerror str returned non string type nonetype

Here is an example of how the error occurs.

main.py
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.

# Use a return statement to return a value from __str__()

To solve the error, use a return statement to return a string from the method.

main.py
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

use return statement to return value from str method

The return value of the __str__() method must be a string object.

# The __str__() method must return a value of type string

If the value you are returning is not a string, use the str() class to convert it.

main.py
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

the str method must return value of type string

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.

main.py
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

dont return an integer from str method

The code sample causes the following error.

shell
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.

main.py
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
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

You can also use the addition (+) operator to concatenate strings.

main.py
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

using addition operator to concatenate strings

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.