Python String and Float Concatenation

In my experience of writing Python code for data analytics firms, I have hit the same wall many times. You try to combine a piece of text with a decimal number, and Python throws a “TypeError.

It is a classic beginner mistake, but even seasoned pros occasionally forget that Python is a strongly typed language.

Unlike some languages that guess what you want to do, Python requires you to be explicit when mixing different data types.

In this tutorial, I will show you exactly how to concatenate a string and a float in Python without crashing your script.

Why Can’t You Directly Concatenate a String and a Float in Python?

Python strictly separates text (strings) and numbers (floats or integers).

If you try to use the plus (+) operator between them, Python gets confused because it doesn’t know if you want to perform math or join text.

I remember building a sales tax calculator for a retail client in California, where this exact issue popped up.

To fix it, we have to convert the float into a string format first.

Method 1: Use the str() Function for Explicit Conversion

This is the most “old-school” way to handle concatenation in Python. I use this when I am working on simple scripts or legacy Python 2 environments.

You wrap the float inside the str() function, which transforms the number into a piece of text.

# Average gas price in Los Angeles
gas_price = 4.89

# Using str() to convert float to string for concatenation
message = "The current gas price in LA is $" + str(gas_price) + " per gallon."

print(message)
# Output: The current gas price in LA is $4.89 per gallon.

You can refer to the screenshot below to see the output.

python concatenate string and float

While this works, it gets messy if you have multiple floats. I find that the code becomes hard to read with all those plus signs and extra spaces.

Method 2: Use Python F-Strings (Recommended)

If you are using Python 3.6 or higher, f-strings are the absolute gold standard for string and float concatenation.

I use f-strings in 99% of my Python projects today because they are incredibly fast and easy to write.

# Interest rate for a home loan in Texas
mortgage_rate = 6.75

# Using f-strings to embed the float directly
display_text = f"The 30-year fixed mortgage rate is currently {mortgage_rate}%."

print(display_text)
# Output: The 30-year fixed mortgage rate is currently 6.75%.

You can refer to the screenshot below to see the output.

concatenate float to string python

F-strings automatically handle the conversion for you. You just place the float variable inside curly braces {}.

It keeps your Python code clean and allows you to format the decimal places at the same time, which is vital for financial data.

Method 3: Use the .format() Method

Before f-strings were released, the .format() method was my go-to tool for Python string manipulation.

It is still very useful today, especially when you want to reuse the same float multiple times in a single sentence.

# Quarterly GDP growth rate for the USA
gdp_growth = 2.1

# Using the .format() method
report = "The reported US GDP growth for Q3 is {}%.".format(gdp_growth)

print(report)
# Output: The reported US GDP growth for Q3 is 2.1%.

You can refer to the screenshot below to see the output.

python concatenate float to string

I often use this method when building dynamic report templates in Python, where the variables are passed in at the end of the string.

Method 4: Format Floats with Precision During Concatenation

Sometimes, your float has way too many decimal places. Imagine a calculation resulting in 3.333333333.

In a professional US business report, you only want to see two decimal places. Python makes this easy to solve during concatenation.

# Calculating a tip at a restaurant in New York City
bill_amount = 85.50
tip_percentage = 0.18
total_tip = bill_amount * tip_percentage # Result: 15.39

# Formatting the float to 2 decimal places using an f-string
final_msg = f"Your total tip amount is ${total_tip:.2f}."

print(final_msg)
# Output: Your total tip amount is $15.39.

You can refer to the screenshot below to see the output.

concatenate string and float python

The .2f tells Python to treat the number as a float and round it to two decimal points. This is a lifesaver for e-commerce Python applications.

Method 5: Use %-Formatting (Legacy Method)

You might see this “printf-style” formatting in older Python tutorials or C-based libraries.

While I don’t use it much in new Python projects, it is important to recognize it when you encounter it in existing codebases.

# Temperature in Miami, Florida
temperature = 82.5

# Using the % operator for concatenation
weather_report = "The current temperature in Miami is %.1f degrees." % temperature

print(weather_report)
# Output: The current temperature in Miami is 82.5 degrees.

In this case, %f acts as a placeholder for the float, and Python replaces it with the actual value.

Method 6: Join Strings and Floats in a List

If you have a collection of data—perhaps a list of prices from a US grocery store—and you want to join them into a single string, you can use the join() method.

However, join() only works with strings, so you must convert the floats first using a Python list comprehension.

# Prices for a standard breakfast combo
item_prices = [2.99, 1.50, 4.25]

# Convert all floats to strings and join them with a comma
price_string = ", ".join([str(price) for price in item_prices])

print("The item prices are: " + price_string)
# Output: The item prices are: 2.99, 1.50, 4.25

This Python technique is very efficient when dealing with large datasets or generating CSV-style output.

Troubleshoot Common Python Errors

When you are concatenating strings and floats, keep an eye out for these two issues:

  1. TypeError: This always means you forgot to convert the float. Double-check your str() or f-string syntax.
  2. AttributeError: This usually happens if you try to call .format() on something that isn’t actually a string.

I always tell my junior developers to print the type() of their variables if they get stuck. It only takes a second and saves minutes of debugging.

Python

# Debugging tip
val = 19.99
print(type(val)) # <class 'float'>

Compare Python Concatenation Methods

MethodSyntax StyleBest For
F-Stringsf"{val}"Modern Python (3.6+), Cleanest code
.format()"{}".format(val)Reusable templates, Python 3
str()"text" + str(val)Quick fixes, Simple scripts
%-Formatting"%f" % valLegacy code, C-style syntax

In this tutorial, you learned several ways to concatenate a string and a float in Python.

I personally recommend using f-strings whenever possible. They are not only more readable but also slightly faster in terms of execution speed within the Python interpreter.

Whether you are building a financial dashboard for a Wall Street firm or a simple weather app, these Python string techniques will serve you well.

The key is to always remember that Python won’t do the conversion for you—you have to tell it exactly how you want that float to look.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.