String Concatenation

String concatenation in Python means joining two or more strings together to form a single string. It is commonly used to combine text, messages, and dynamic values in programs.

What Is String Concatenation?

String concatenation allows you to:
  • Combine multiple strings
  • Create meaningful messages
  • Build dynamic output
Python provides multiple ways to concatenate strings.
Definition

String concatenation is the process of joining strings together.


Using the + Operator

The most common way to concatenate strings is using the + operator.
python
a = "Hello"
b = "Python"
print(a + b)
python
first = "Code"
second = "Marathi"
print(first + " " + second)
python
print("AWS" + " Cloud")
Important

Both operands must be strings when using +.


Concatenating Multiple Strings

You can join more than two strings using the + operator.
python
a = "Python"
b = "is"
c = "easy"
print(a + " " + b + " " + c)
python
lang = "Python"
version = "3"
print(lang + " " + version)
python
print("Learn " + "Python " + "Today")

Using Comma in print() (Not True Concatenation)

Using commas in print() places values side by side with a space.
python
a = "Hello"
b = "Python"
print(a, b)
python
name = "John"
age = 25
print(name, age)
python
print("Sum is", 10 + 20)
Key Difference

Comma does not create a single string; it only formats output.


Concatenation Using f-Strings (Recommended)

f-strings provide a clean and modern way to combine strings and variables.
python
name = "Python"
print(f"Welcome to {name}")
python
course = "AWS"
level = "Beginner"
print(f"{course} Course - {level}")
python
a = 10
b = 20
print(f"Sum is {a + b}")
Best Practice

f-strings are the best and safest way to concatenate strings.


Using format() Method

The format() method is another way to concatenate strings.
python
name = "Python"
print("Welcome to {}".format(name))
python
a = 5
b = 10
print("Sum is {}".format(a + b))
python
print("{} is awesome".format("Python"))

Repeating Strings (* Operator)

You can repeat strings using the * operator.
python
print("Python " * 3)
python
star = "*"
print(star * 10)
python
print("Hi " * 2)

Common Errors in String Concatenation

Mixing String and Number with +

python
age = 25
print("Age is " + age)  # Error

Correct Way

python
age = 25
print("Age is " + str(age))

Summary

  • String concatenation joins strings together
  • + operator joins strings directly
  • Comma in print() is not true concatenation
  • f-strings are recommended
  • format() is an alternative method
  • Strings can be repeated using *

Exercise

  • Concatenate your first and last name
  • Print a sentence using f-string
  • Repeat a word three times