0
0
PythonHow-ToBeginner · 3 min read

How to Concatenate Strings in Python: Simple Guide

In Python, you can concatenate strings using the + operator, which joins two or more strings together. Alternatively, use str.join() to combine a list of strings or f-strings for formatted string concatenation.
📐

Syntax

Here are the common ways to concatenate strings in Python:

  • string1 + string2: Joins two strings directly.
  • ' '.join(list_of_strings): Joins multiple strings from a list with a separator.
  • f"{var1} {var2}": Uses variables inside strings with formatting.
python
result = string1 + string2
result = ' '.join(list_of_strings)
result = f"{var1} {var2}"
💻

Example

This example shows three ways to concatenate strings: using +, join(), and f-strings.

python
name = "Alice"
greeting = "Hello"

# Using + operator
message1 = greeting + ", " + name + "!"

# Using join()
words = ["Hello", name, "!"]
message2 = ' '.join(words)

# Using f-string
message3 = f"{greeting}, {name}!"

print(message1)
print(message2)
print(message3)
Output
Hello, Alice! Hello Alice ! Hello, Alice!
⚠️

Common Pitfalls

Common mistakes when concatenating strings include:

  • Trying to concatenate strings with non-string types without converting them first.
  • Using + in a loop, which can be inefficient for many strings.
  • Forgetting spaces when concatenating manually.

Always convert non-string values with str() and prefer join() for multiple strings.

python
age = 30

# Wrong: causes error
# message = "Age: " + age

# Right: convert number to string
message = "Age: " + str(age)

# Inefficient in loop (legacy)
words = ["Python", "is", "fun"]
result = ""
for word in words:
    result += word + " "  # slower for many words

# Efficient way
result = ' '.join(words)
print(message)
print(result)
Output
Age: 30 Python is fun
📊

Quick Reference

MethodDescriptionExample
+ operatorJoins two or more strings"Hello" + " World"
join()Joins list of strings with separator'-'.join(["a", "b", "c"] )
f-stringsEmbed variables inside stringsf"Name: {name}"

Key Takeaways

Use the + operator to join two or more strings simply.
Use str.join() to efficiently concatenate many strings from a list.
Use f-strings to combine strings with variables cleanly and readably.
Always convert non-string types to strings before concatenation.
Avoid using + in loops for many strings; prefer join() for better performance.