How to Concatenate String and Float in Python?

Recently, I was working on a financial application in Python where I wanted to concatenate string and float. I tried three methods. In this tutorial, I will show you how to concatenate string and float in Python using various methods with examples.

To concatenate a string and a float in Python, you can use the str() function to convert the float to a string. For example, if you have city = "New York" and temperature = 75.5, you can concatenate them using result = city + " has a temperature of " + str(temperature) + " degrees Fahrenheit.".

Concatenate String and Float in Python

In Python, you can use various methods to concatenate a string and a float. Let me show you each method with examples.

Method 1: Using the str() Function

The best way to concatenate a string and a float in Python is by converting the float to a string using the str() function. This method ensures that both operands are of the same type (string). Here is an example to understand it better.

Example:

city = "New York"
temperature = 75.5

result = city + " has a temperature of " + str(temperature) + " degrees Fahrenheit."
print(result)

Output:

New York has a temperature of 75.5 degrees Fahrenheit.

You can also see the exact output in the screenshot below after I executed the above Python code using an Online Python editor.

Concatenate String and Float in Python

Read How to use Pandas to convert float to int in Python

Method 2: Using String Formatting

Python offers several ways to format strings like the format() method and f-strings. We can use these methods to concatenate a string with a float in Python.

Using format() Method:

The format() method allows you to insert variables into a string template. Here is an example.

Example:

city = "Los Angeles"
temperature = 80.3

result = "{} has a temperature of {} degrees Fahrenheit.".format(city, temperature)
print(result)

Output:

Los Angeles has a temperature of 80.3 degrees Fahrenheit.

I executed the above Python code, and you can see the output in the screenshot below:

Concatenate Strings and Floats in Python

Using f-Strings:

Introduced in Python 3.6, f-strings provide a more concise and readable way to format strings. Here is an example.

Example:

city = "Chicago"
temperature = 70.2

result = f"{city} has a temperature of {temperature} degrees Fahrenheit."
print(result)

Output:

Chicago has a temperature of 70.2 degrees Fahrenheit.

Read How to convert float to int Python

Method 3: Using the join() Method

The join() method can also be used to concatenate strings and floats in Python. However, you must first convert the float to a string. Let me show you an example to help you understand.

Example:

city = "Houston"
temperature = 85.7

result = " ".join([city, "has a temperature of", str(temperature), "degrees Fahrenheit."])
print(result)

Output:

Houston has a temperature of 85.7 degrees Fahrenheit.

After I executed the above Python code, you can see the output in the screenshot below:

Python Concatenate String and Float

Method 4: Using print() with Multiple Arguments

While this doesn’t directly concatenate strings and floats into a single string, using print() with multiple arguments can be useful for quick output formatting.

Here is an example.

Example:

city = "San Francisco"
temperature = 65.1

print(city, "has a temperature of", temperature, "degrees Fahrenheit.")

Output:

San Francisco has a temperature of 65.1 degrees Fahrenheit.

Conclusion

In this tutorial, I have explained how to concatenate a string and a float in Python using different methods like the str() function, string formatting, or using f-strings, etc.

I recommend using the str() functions for concatenating strings and floats in Python.

You may like the following tutorials:

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.