4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to concatenate Strings on Python

By 4Geeks

Join text, variables, numbers and lists with +, f-strings and join(), and avoid the usual concatenation errors.

6 min readLast updated
Concatenate strings in Python icon

How do you concatenate strings in Python?

Short answer: use the + operator to join two or three strings, an f-string when you mix text with variables or numbers, and join() when you have a list of strings:

python
name = "Ana"
age = 30
 
print("Hello, " + name)                 # Hello, Ana
print(f"{name} is {age} years old")     # Ana is 30 years old
print(" ".join(["Learn", "Python"]))    # Learn Python

+ only joins text: to add a number, convert it with str() or use an f-string.

The + operator only joins text. f-strings convert numbers for you, and join() combines a whole list with the separator you choose.

Ways to concatenate strings in Python

MethodExampleUse it to
+"Hello, " + nameJoin a few strings
+=text += "!"Add text to a variable
f-stringf"{name} is {age} years old"Mix text, variables and numbers
join()" ".join(words)Join a list with a separator
format()"{} is {} years old".format(name, age)Older code or reusable templates
%"%s is %d years old" % (name, age)Read very old code
*"-" * 20Repeat a string

Concatenate with the + operator

python
name = "Alex"
 
print("My name is " + name + " and I'm a developer")
# My name is Alex and I'm a developer

+ does not add spaces. If you forget the space after is, you get My name isAlex.

Add text with +=

python
greeting = "Hello"
greeting += ", world"
 
print(greeting)  # Hello, world

Python strings cannot be changed: += creates a new string and stores it in the same variable.

Concatenate with f-strings

Put an f before the quotes and write the variables inside curly braces. You can also do math and formatting inside the braces:

python
product = "Keyboard"
price = 20
 
print(f"{product}: ${price * 1.08:.2f} with tax")
# Keyboard: $21.60 with tax

f-strings convert numbers to text for you and are the most readable option in modern Python.

Join a list with join()

python
words = ["Learn", "to", "code"]
 
print(" ".join(words))   # Learn to code
print("-".join(words))   # Learn-to-code
print("\n".join(words))  # one word per line

The string before .join() is the separator. join() only accepts text: if the list has numbers, convert them first with map(str, items):

python
print(", ".join(map(str, [1, 2, 3])))  # 1, 2, 3

To learn more, see how to convert a list to a string in Python.

Concatenate numbers and text

python
age = 30
 
print("You are " + str(age) + " years old")  # You are 30 years old
print(f"You are {age} years old")            # You are 30 years old

Without str(), "You are " + age raises TypeError: can only concatenate str (not "int") to str.

format(), % and *

python
name = "Ana"
age = 30
 
print("{} is {} years old".format(name, age))  # Ana is 30 years old
print("%s is %d years old" % (name, age))      # Ana is 30 years old
print("ha" * 3)                                # hahaha

format() and % still work, but in new code an f-string reads better. * repeats a string as many times as you ask.

Career · AI Engineering

You can join strings, now build prompts and AI apps

From zero to junior AI Engineer: Python, data, AI agents and RAG with mentors and real projects.

Concatenate many strings in a loop

If you are joining many pieces, store them in a list and join them at the end with join(). It is clearer and more efficient than repeating += thousands of times:

python
parts = []
for i in range(3):
    parts.append(f"line {i}")
 
print("\n".join(parts))
# line 0
# line 1
# line 2

Join long strings across several lines

Two string literals written next to each other inside parentheses are joined automatically, without +:

python
message = ("Learn to code "
           "with Python")
 
print(message)  # Learn to code with Python

Use string concatenation with AI

When you code with AI, you concatenate strings all the time: the prompts you send to a model are built with f-strings and join():

python
text = "Python is a programming language widely used in AI."
n = 1
 
prompt = f"Summarize this text in {n} sentence: {text}"
print(prompt)
# Summarize this text in 1 sentence: Python is a programming language widely used in AI.

An assistant like ChatGPT, Claude or GitHub Copilot also helps you write and fix that code. These three prompts are ready to copy.

1. Pick the best way

I have these variables in Python: [paste your variables and their values]
I want to get this text: [write the result you expect]
Write it with an f-string and, if there is a list, with join(). Explain why.

2. Fix a concatenation error

When I concatenate strings in Python I get this error: [paste the full error]
This is my code: [paste your code]
Explain the cause and give me the fix.

3. Build a prompt for an AI model

Help me write a Python function that takes [a text and a number of sentences]
and returns a prompt for an AI model using f-strings. Show me an example call.

Always check the answer by running it in your terminal or in Jupyter.

Learn with a mentor

From joining text to AI applications

In the 4Geeks AI Engineering career program you start with no experience and finish with AI agents and RAG systems in your portfolio.

Common errors when concatenating strings

What you seeWhy it happensFix
TypeError: can only concatenate str (not "int") to strYou are joining text and a number with +Use str(number) or an f-string
TypeError: unsupported operand type(s) for +: 'int' and 'str'The number comes first in the sumSame: str(number) or an f-string
TypeError: sequence item 1: expected str instance, int foundThe list passed to join() has numbersUse ", ".join(map(str, items))
AttributeError: 'list' object has no attribute 'join'You wrote items.join(" ")The separator goes first: " ".join(items)
{name} is printed literallyYou forgot the f before the quotesWrite f"{name}"
Words come out stuck together, like HelloAna+ does not add spacesAdd the space inside the string or use " ".join(...)
Free download

Concatenating strings in a 2-page cheat sheet

Every way to join text in Python, when to use each one, ready-to-copy recipes, the most common errors and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

Concatenate strings in Python cheat sheet

2 pages · PDF · 4Geeks

  • +, f-strings, join(), format(), % and * in one table
  • Recipes for numbers, lists and AI prompts
  • The 6 most common errors and prompts to fix them with AI

Get it free

Your PDF right away

Frequently asked questions about concatenating strings in Python

Want to go from writing Python to building AI apps, with real projects and mentors?

See the AI Engineering program