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!
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
- Python copy file from a source to destination - Python
- How to display directory tree in Mac Terminal - MacOS
- Android R Cannot Be Resolved To A Variable - Android
- How to turn off Facebook autoplay videos on timeline - Facebook
- Write to File in Java using BufferedWriter - Java
- PowerShell: How to Check if a Module is Already Installed - Powershell
- Sign in as different user missing in SharePoint 2013 how to enable - SharePoint
- How to rerun last command in Zsh shell - HowTos