TypeError: can only concatenate str (not "float") to str

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: can only concatenate str (not "float") to str #

The Python "TypeError: can only concatenate str (not "float") to str" occurs when we try to concatenate a string and a float. To solve the error, convert the float to a string, e.g. str(my_float) to concatenate the strings or convert the str to a float, e.g. float(my_str).

typeerror can only concatenate str not float to str

Here is an example of how the error occurs.

main.py
# ⛔️ TypeError: can only concatenate str (not "float") to str result = 'it costs: ' + 59.49
We tried to use the addition (+) operator to concatenate a string and a float which caused the error.

The values on the left and right-hand sides need to be of compatible types.

One way to solve the error is to convert the float to a string.

main.py
result = 'it costs: ' + str(59.49) print(result) # 👉️ it costs: 59.49

We passed the floating-point number to the str() class and converted it to a string before concatenating the two strings.

If you have a number that is wrapped in a string and a float, you need to convert the string to a float (or an integer) to add the two numbers.

main.py
result = float('10.51') + 59.49 print(result) # 👉️ 70.0

We passed the string to the float() class to convert it to a floating-point number. Note that you can also use the int() class if you need to convert a string to an integer.

IMPORTANT: if you use the input() built-in function, all of the values the user enters get converted to strings (even numeric values).

An alternative to concatenating strings with the addition (+) operator is to use a formatted string literal.

main.py
str_1 = 'it costs: ' num_1 = 59.49 result = f'{str_1} {num_1} usd' print(result) # 👉️ 'it costs: 59.49 usd'
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}.

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
str_1 = 'it costs: ' print(type(str_1)) # 👉️ <class 'str'> print(isinstance(str_1, str)) # 👉️ True num_1 = 59.49 print(type(num_1)) # 👉️ <class 'float'> print(isinstance(num_1, float)) # 👉️ True

The type class returns the type of an object.

The error often occurs when getting user input using the built-in input() function.

main.py
num = input('Enter your fav number: ') print(num) # 👉️ '13.7' print(type(num)) # 👉️ <class 'str'> result = num + 16.3 # ⛔️ TypeError: can only concatenate str (not "float") to str print(result)

The input function converts the data to a string and returns it.

Even if the user enters a number, it gets converted to a string before it is returned from input().

If you need to convert the result to a number, pass it to the float() (or int()) class.

main.py
num = input('Enter your fav number: ') print(num) # 👉️ '13.7' print(type(num)) # 👉️ <class 'str'> # ✅ convert to float result = float(num) + 16.3 print(result) # 👉️ 30.0

We converted the string we got from the input function back to a float and added the two numbers.

The values we are adding with the addition (+) operator must be of compatible types, e.g. a float and an int.

We can't simply add a string and a float using the addition (+) operator as that causes the error.

Conclusion #

The Python "TypeError: can only concatenate str (not "float") to str" occurs when we try to concatenate a string and a float. To solve the error, convert the float to a string, e.g. str(my_float) to concatenate the strings or convert the str to a float, e.g. float(my_str).

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.