![Python TypeError - can only concatenate str (not int) to str Python TypeError - can only concatenate str (not int) to str](https://code2care.org/python/fix-type-error-can-only-concatenate-str-not-int-to-str-in-python/images/Python TypeError - can only concatenate str (not int) to str.png)
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!
![Fix - Python TypeError - can only concatenate str (not int) to str Fix - Python TypeError - can only concatenate str (not int) to str](https://code2care.org/python/fix-type-error-can-only-concatenate-str-not-int-to-str-in-python/images/Fix - Python TypeError - can only concatenate str (not int) to str.png)
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
- Enable Dark Mode in Google Search - Google
- How to show line numbers in Nano on Mac - MacOS
- How to resolve Failed to create interpreter PyCharm Error - Python
- PowerShell: Steps to Connect to connect to Exchange Online - Powershell
- Microsoft Teams Error - You cannot add another work account to Teams at the moment - Teams
- Python matplotlib segmentation fault: 11 macOS Big Sur - Python
- What is the doctype for HTML5? - Html
- [Eclipse] Syntax error, annotations are only available if source level is 1.5 or greater - Eclipse