Python provides different means of joining strings, each with different benefits. Here’s all you need to know to concatenate strings in Python.

How to Concatenate Strings in Python

Python offers several ways of concatenating strings. Here are four of the most common: three operators and a function. You can use the join function or one of the +, %, or * operators. Each has its use, depending on your circumstances.

1. Using the + Operator

You can use the + operator to add two numbers, but you can also use it to combine two strings. Here’s a basic example showing how to do so:

The resulting output will be:

This example joins two strings, but you can join as many as you want in one statement:

The resulting output will be:

2. Using the Join() Method

The join method is one of Python’s most useful built-in functions. It applies to strings only so you can’t use it to add numeric values. You should use the join method when you want to combine a set of strings with the same string in-between: the separator. The separator can be any valid string, including the empty string:

The output is:

3. Using the % Operator

The % operator formats a string, but in its simplest form, it serves as a concatenating tool. You can use the %s placeholder to substitute for strings, effectively concatenating them.

For example:

The output is:

Python 3 introduced a new function for string formatting: the format method. This works similarly, substituting placeholders for values in a larger string, and you can use it for basic concatenation too.

4. Using the * Operator

The * operator provides another means of concatenating strings, albeit for very specific cases. This operator repeats a string a certain number of times. For example:

The output is:

Adding Text Strings Together With Python Concatenation

Python is a powerful language since it has built-in operators and methods for many common operations. By mastering these, you can use the most appropriate for whatever situation you find yourself in.

String formatting is a fundamental part of programming in Python. There are many Python courses available to help budding programmers learn the basics.