How to Concatenate Strings in Python

What you’ll build or solve

You’ll combine strings in Python using the most common approaches: +, join(), and f-strings.

When this approach works best

This approach works best when you:

  • Build user-facing messages, like "Hello, Mina!" or status updates.
  • Combine data into a label or key, like "order-" + order_id.
  • Generate output from lists, like turning ["a", "b", "c"] into "a,b,c".

Avoid this approach when:

  • You are building huge text in a tight loop. Prefer collecting pieces in a list and using join().

Prerequisites

  • Python installed
  • You know what a string and a list are

Step-by-step instructions

1) Concatenate with + for simple cases

Use + when you have a few strings to combine.

first = "Ada"
last = "Lovelace"

full_name = first + " " + last
print(full_name)

If you want to add punctuation or spacing, include it as a string:

message = "Hello, " + first + "!"
print(message)

2) Use f-strings for readable formatting

f-strings let you embed values inside a string. This is often the clearest choice for messages.

name = "Mina"
score = 92

message = f"{name} scored {score} points."
print(message)

What to look for: f-strings convert values to text for you, so you don’t need str(score).


3) Use join() to concatenate many pieces

If you have a list of strings, join() is usually the best option.

words = ["learn", "python", "today"]
sentence = " ".join(words)
print(sentence)

You can pick any separator:

tags = ["python", "data", "web"]
csv = ",".join(tags)
print(csv)

What to look for: All items must be strings. Convert first if needed.


4) Convert non-strings before concatenating

If you use + with a non-string, Python raises a TypeError. Convert values with str() or switch to an f-string.

count = 3

label = "items: " + str(count)
print(label)

Using an f-string is often simpler:

count = 3
label = f"items: {count}"
print(label)

5) Build text in a loop using a list, then join()

When you concatenate repeatedly in a loop, collect parts first, then join once.

names = ["Ada", "Mina", "Sam"]
parts = []

for name in names:
    parts.append(f"- {name}")

result = "\n".join(parts)
print(result)

This stays fast and keeps your code easy to read.


Examples you can copy

Example 1: Build a greeting

first_name = "Sam"
greeting = "Hi " + first_name + "!"
print(greeting)

Example 2: Create a filename from pieces

user_id = 42
date = "2026-02-12"

filename = f"user-{user_id}-{date}.txt"
print(filename)

Example 3: Join a list into a comma-separated string

cities = ["Boston", "New York", "Chicago"]
line = ", ".join(cities)
print(line)

Example 4: Join non-strings by converting first

This example uses a generator expression to convert numbers to strings.

numbers = [1, 2, 3, 4]
text = " - ".join(str(n) for n in numbers)
print(text)

Common mistakes and how to fix them

Mistake 1: Concatenating a string with an int

What you might do:

count = 5
message = "Total: " + count
print(message)

Why it breaks: + only works between strings, not between a string and an integer.

Correct approach: Convert or use an f-string.

count = 5
message = "Total: " + str(count)
print(message)

count = 5
message = f"Total: {count}"
print(message)

Mistake 2: Using join() on a list that contains non-strings

What you might do:

items = ["A", 2, "C"]
text = ",".join(items)
print(text)

Why it breaks: join() requires every item to be a string.

Correct approach: Convert items first.

items = ["A", 2, "C"]
text = ",".join(str(x) for x in items)
print(text)

Troubleshooting

If you see TypeError: can only concatenate str (not "int") to str, do this: convert with str(value) or use an f-string.

If you see TypeError: sequence item 1: expected str instance, do this: convert items before join(), like str(x) for x in items.

If your output has missing spaces, do this: add separators explicitly, like " ", ", ", or "\n".

If concatenation feels slow in a loop, do this: append to a list and call join() once at the end.

If you see extra separators at the end, do this: avoid manual += "," patterns, and use join().


Quick recap

  • Use + to concatenate a few strings.
  • Use f-strings for readable messages with variables.
  • Use join() to concatenate many strings from a list.
  • Convert non-strings with str() or use f-strings.
  • For loops, collect parts in a list, then call join() once.