← Back to Chapters

Python String Concatenation

? Python String Concatenation

⚡ Quick Overview

String concatenation means combining two or more strings together to form a single string. In Python, you can concatenate strings in several ways:

  • Using the + operator.
  • Using f-strings (formatted string literals).
  • Using the format() method.
  • Using join() to join items from an iterable like a list.

? Key Concepts

  • Concatenation creates a new string from existing strings.
  • Python strings are immutable, so each concatenation builds a new string.
  • All parts must be strings; use str() if you want to combine numbers with text.
  • f-strings and format() help embed variables inside strings cleanly.
  • join() is efficient when combining many strings from a list or other iterable.

? Syntax and Theory

➕ Using the + Operator

The simplest way to concatenate strings is with the + operator. You can also add spaces manually where needed.

? Using f-Strings

f-Strings (available from Python 3.6+) are written by prefixing a string with f and placing variables or expressions inside { }. They are very readable and efficient.

⚙️ Using format()

The format() method replaces { } placeholders inside a string with provided values in order.

? Using join() for Lists

The join() method is called on a separator string (like " ") and takes an iterable of strings (like a list) to combine them into one string.

? Code Examples

➕ Example: Concatenation with +

? View Code Example
first = "Hello"
second = "World"
result = first + " " + second
print(result)  # Hello World

? Example: Using f-Strings (Recommended)

? View Code Example
name = "Alice"
age = 25
message = f"My name is {name} and I am {age} years old."
print(message)

⚙️ Example: Using format()

? View Code Example
name = "Alice"
age = 25
message = "My name is {} and I am {} years old.".format(name, age)
print(message)

? Example: Joining Strings from a List

? View Code Example
words = ["Python", "is", "awesome"]
sentence = " ".join(words)
print(sentence)  # Python is awesome

? Live Output and Explanation

?️ What gets printed?

  • Using +: Hello World
  • Using f-string: My name is Alice and I am 25 years old.
  • Using format(): My name is Alice and I am 25 years old.
  • Using join(): Python is awesome

All four examples build new strings from smaller pieces. Notice how f-strings and format() handle variables neatly without needing explicit + and str() conversions for numbers.

? Tips & Best Practices

  • Prefer f-strings for readable and efficient concatenation with variables.
  • Use join() when combining many strings from a list or other iterable.
  • Remember to add spaces manually when using +, for example "Hello " + name.
  • Do not concatenate strings and numbers directly; convert numbers with str() or use f-strings: f"Age: {age}".
  • Keep your concatenation expressions simple so they are easy to read and debug.

? Try It Yourself

  • Concatenate a first name and last name with a space between them.
  • Use an f-string to create a greeting that includes your name and age.
  • Create a list of words and join them into a sentence using " ".join(...).
  • Fix an error where a string and a number were concatenated directly by using str() or an f-string.