
TypeError error will occur when you try to concatenate a string and an integer in your Python.
This is because when you have a string and an int value (even when you are doing it in a print() function) Python will consider it as an addition and thus will not allow adding a string to an integer as they are two different data types.
Error Code Example:name = "Sam"
age = 23
print("Hello " + name + " you are " + age + " years old!")
Error:
Traceback (most recent call last):
File "/Users/code2care/PycharmProjects/python-learnings/venv/0-test.py", line 4, in <module>
print("Hello "+ name + "you are " + age + "year old!")
TypeError: can only concatenate str (not "int") to str
Fix:
All you need to do is wrap your numerical int value with a str() to convert it into a string first so that it is considered as a "string concatenation" and not ints
Example: Code fixname = "Sam"
age = 23
print("Hello "+ name + " you are " + str(age) + " years old!")
Output:
Hello Sam you are 23 years old!

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!