TypeError: can only concatenate str (not “NoneType”) to str in Python

TypeError: can only concatenate str (not “NoneType”) to str in Python

If you are getting trouble with the error “TypeError: can only concatenate str (not “NoneType”) to str” in Python, do not worry. Today, our article will give some solutions and detailed explanations to handle the problem. Keep on reading to get your answer.

Why does the error “TypeError: can only concatenate str (not “NoneType”) to str” in Python occur?

TypeError is an exception when you apply a function or operation to objects of the wrong type. “TypeError: can only concatenate str (not “NoneType”) to str” in Python occurs when you try to concatenate a variable that has the type None and a variable that belongs to the string type. Look at the simple example below that causes the error.

Code:

myStr = "string"
myNone = None

print(myStr + myNone)

Result:

TypeError Traceback (most recent call last)
 in <module>
----> 4 print(myStr + myNone)
TypeError: can only concatenate str (not "NoneType") to str

This is a very simple example to show you the error. We all know that we can not apply operators to different variables in the Python programming language. In this situation, we can join a string type and a None Type. 

Now you are clear about the root of the problem. We will look at another example causing the error people often get when working with Object Oriented Programming.

Code:

# Create Class student to store name and age
class Student:
  def __init__(self, name, age):
    self.__name = name
    self.__age = age
 
  def showName(self):
    print(self.__name)
 
  def showAge(self):
    print(self.__age)
  
student1 = Student("Helen Kang", 15)
print("My name is" + student1.showName())

Result:

Helen Kang
Traceback (most recent call last):
  line 14, in <module>
    print("My name is" + student1.showName())
TypeError: can only concatenate str (not "NoneType") to str

Let’s move on. We will discover solutions to this problem.

Solutions to the problem

Add a new function

As you can see, the method in the example is a non-return function because they return nothing but print the string. When called, the method returns nothing, and its result belongs to the class NoneType. Let’s check the type of the function showName().

Code:

print(type(student1.showName()))

Result:

<class 'NoneType'>

We need to create a new method that returns the name to fix the error. This method will be valid to be called in the print function. 

Code:

# Create Class student to store name and age
class Student:
  def __init__(self, name, age):
    self.__name = name
    self.__age = age
 
  def showName(self):
    print(self.__name)
 
  def showAge(self):
    print(self.__age)
  
  def getName(self):
    return self.__name
  
student1 = Student("Helen Kang", 15)
print("My name is " + student1.getName())

Result:

My name is Helen Kang

You can also use the comma or format string to get the same result:

print("My name is", student1.getName())
print(f"My name is {student1.getName()}")

Call the function without the print function

Because the function is to print the name, you do not need to call it in the print function. We will only print the introduction string and call the function to print the name alone. To not print the new line, use syntax end = ‘ ‘.

Code:

# Create Class student to store name and age
class Student:
  def __init__(self, name, age):
    self.__name = name
    self.__age = age
 
  def showName(self):
    print(self.__name)
 
  def showAge(self):
    print(self.__age)
    
student1 = Student("Helen Kang", 15) 
print("My name is", end = ' ')
student1.showName()

Result:

My name is Helen Kang

Summary

Our article has explained the error “TypeError: can only concatenate str (not “NoneType”) to str” in Python and showed you the root of the problem. Understanding each function’s returned type is necessary to avoid errors.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *