Tutorial

How to Concatenate String and Int in Python (With Examples)

Clear, practical guide: how to concatenate string and int in python using str(), f-strings, format(), % and print(), plus examples and common fixes.

Drake Nguyen

Founder · System Architect

3 min read
How to Concatenate String and Int in Python (With Examples)
How to Concatenate String and Int in Python (With Examples)

Introduction

If you need to concatenate string and int in python, you’ll find several safe and readable approaches. Python disallows adding a str and an int directly with the + operator—doing so raises a TypeError. This guide shows common methods (and when to use them) so you can combine text and numbers without errors.

Prerequisites

  • Basic Python knowledge (variables, print, functions).
  • Python 3.6+ is recommended to use f-strings, though older methods still work.

Why the TypeError occurs

Python keeps types explicit: "hello" + 5 fails because str and int are different types. The error message you’ll see is usually: TypeError: can only concatenate str (not "int") to str. Fix it by converting the integer to a string or using a formatting method that handles conversion for you.

Methods to concatenate string and int in python

1) Convert with str()

The simplest, most explicit approach is to convert the number using str() before concatenation.

year_msg = "Year is "
current_year = 2018
print(year_msg + str(current_year))  # Output: Year is 2018

2) Use f-strings (recommended for Python 3.6+)

F-strings provide readable interpolation and automatically convert values to strings when formatting.

current_year = 2018
print(f"Year is {current_year}")  # Output: Year is 2018

3) str.format()

str.format() is flexible and works well when you want explicit placeholders.

print("Year is {}".format(2018))  # Output: Year is 2018

4) Percent (%) formatting

The printf-style % operator still works but is older and less flexible than f-strings or format().

print("Year is %s" % 2018)  # Output: Year is 2018

5) Using print() with commas

When printing, separate arguments with commas; Python inserts spaces and converts non-strings automatically. This is useful for quick output but not for building reusable string values.

print("You have", 5, "new messages")  # Output: You have 5 new messages

6) Joining multiple parts

If you want to use str.join(), make sure every element is a string first. This can be handy for building longer strings from lists.

parts = ["log_", str(1643723400), ".txt"]
file_name = "".join(parts)  # Output: log_1643723400.txt

Practical examples and use cases

Dynamic messages

count = 5
message = f"You have {count} new messages"
print(message)  # Output: You have 5 new messages

Log lines and filenames

timestamp = 1643723400
error_code = 500
log_message = "Error occurred at timestamp: " + str(timestamp) + " with code: " + str(error_code)
print(log_message)
# Or with f-strings:
print(f"Error occurred at timestamp: {timestamp} with code: {error_code}")

Common errors and how to fix them

  • TypeError: can only concatenate str (not "int") to str — convert the int with str() or use f-strings/formatting.
  • Forgetting to cast before join() — ensure every element in the iterable is a string.
  • Using + incorrectly in print() — remember print() accepts multiple args separated by commas and converts them automatically.

Pros and cons (quick comparison)

  • str(): Explicit conversion; good for simple cases. Slightly verbose for many values.
  • f-strings: Most readable and efficient for Python 3.6+; best for inline expressions.
  • str.format(): Flexible and readable; useful when building templates.
  • % formatting: Legacy support; less recommended for new code.
  • print with commas: Quick for console output but not for returning a single string value.

Frequently asked questions

How to concatenate string and int in python without error?

Convert the integer using str(), use an f-string, or apply str.format(). Any of these prevents the TypeError and produces the combined string.

How Netalith I fix "TypeError: can only concatenate str (not 'int') to str"?

Cast the integer to a string with str() or use a formatting approach like f-strings which implicitly converts values when formatting.

Is there a best way to concatenate strings and numbers in Python?

For modern Python (3.6+), f-strings are usually best: concise, readable, and fast. For compatibility with older interpreters, use str() or str.format().

Conclusion

Concatenating strings and integers in Python is straightforward once you choose the right method. Use str() for explicit conversions, f-strings for modern, readable code, and format() or % when appropriate for compatibility. Always convert non-strings before using the + operator or a join to avoid TypeError.

Tip: When building user-facing messages or file names, prefer f-strings for clarity and maintainability.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.