Last modified: Feb 08, 2026 By Alexander Williams

Python Convert Number to String | str() Method Guide

Converting numbers to strings is a basic Python skill. You need it for output, file writing, and data formatting. This guide explains the best methods.

We will cover the str() function, f-strings, and the format() method. Each has its own use case and advantages.

Why Convert Numbers to Strings?

Python treats numbers and strings as different data types. You cannot combine them directly. Trying to do so causes a TypeError.


# This will cause an error
age = 25
message = "I am " + age + " years old."
    

Traceback (most recent call last):
  File "", line 2, in 
TypeError: can only concatenate str (not "int") to str
    

To fix this, you must convert the number to a string first. This is called type casting. It is essential for creating user messages, logging, and building file paths.

It is also crucial when preparing data for storage in text files or databases. Just like when you need to Python Convert Images Between File Formats, converting data types is a fundamental processing step.

Method 1: The str() Function

The str() function is the most direct way. It takes any object and returns its string representation.


# Convert an integer
my_number = 42
string_number = str(my_number)
print(string_number)
print(type(string_number))

# Convert a float
pi = 3.14159
string_pi = str(pi)
print(string_pi)
    

42

3.14159
    

This method is simple and reliable. It works for integers, floats, and even complex numbers. The output string looks exactly like the number.

Use str() for simple, straightforward conversions where you don't need special formatting.

Method 2: Formatted String Literals (f-strings)

F-strings (introduced in Python 3.6) are a modern and powerful tool. They let you embed expressions inside string literals.


name = "Alice"
score = 95.5

# The number is automatically converted within the f-string
message = f"{name} scored {score} points."
print(message)

# You can also perform formatting inside the braces
formatted_message = f"The price is ${score:.2f}"
print(formatted_message)
    

Alice scored 95.5 points.
The price is $95.50
    

F-strings are very readable and efficient. They handle the conversion for you. You can also control formatting, like the number of decimal places.

This approach is similar in spirit to using a preprocessor like Python convert Sass to Css, where you write in a more expressive way to generate the final output.

Method 3: The format() Method

The format() method is versatile and available in older Python versions. It uses placeholders {} in a string.


temperature = -5.75
wind_speed = 22

# Basic substitution
report = "Temp: {}°C, Wind: {} km/h".format(temperature, wind_speed)
print(report)

# Using positional and formatting arguments
detailed_report = "Temp: {0:.1f}°C, Wind: {1} km/h".format(temperature, wind_speed)
print(detailed_report)
    

Temp: -5.75°C, Wind: 22 km/h
Temp: -5.8°C, Wind: 22 km/h
    

This method gives you fine-grained control. You can specify the order of arguments and apply complex formatting rules. It is excellent for creating templates.

Handling Different Number Types

Python has several numeric types. The conversion methods work for all of them.


# Integer
int_num = 100
print(str(int_num))  # '100'

# Float
float_num = 9.81
print(str(float_num)) # '9.81'

# Complex Number
complex_num = 2+3j
print(str(complex_num)) # '(2+3j)'
    

The conversion is generally lossless for integers. For floats, the string represents the stored value, which might have precision limits.

Always be aware of floating-point precision when converting to and from strings.

Common Use Cases and Examples

Let's look at practical examples where number-to-string conversion is essential.


# 1. Building File Paths
user_id = 4572
filename = f"data_report_{user_id}.txt"
print(f"Saving to: {filename}")

# 2. User-Friendly Console Output
items = 3
cost = 19.99
total = items * cost
receipt = f"You bought {items} items for ${cost} each. Total: ${total:.2f}"
print(receipt)

# 3. Writing Data to a Text File
data = [10, 20.5, 30]
with open("output.txt", "w") as file:
    for value in data:
        file.write(str(value) + "\n") # Must convert to string to write
print("Data written to file.")
    

Saving to: data_report_4572.txt
You bought 3 items for $19.99 each. Total: $59.97
Data written to file.
    

Conclusion

Converting numbers to strings in Python is simple but vital. The str() function is your go-to for basic needs.

For creating messages with embedded variables, use f-strings. They are clean and powerful. The format() method remains a solid choice for complex formatting and compatibility.

Mastering these conversions will help you avoid errors and write cleaner, more effective Python code. Whether you're generating reports, saving data, or building user interfaces, this skill is indispensable.