Sling Academy
Home/Python/Python: 8 Ways to Concatenate Strings

Python: 8 Ways to Concatenate Strings

Last updated: May 27, 2023

String concatenation is the process of combining two or more strings into a single string. This pithy, example-based article shows you a couple of different ways to concatenate strings in Python.

Using the + operator

This approach is simple, intuitive, and widely used. A new string will be created. You can assign it to a variable or use it directly.

Example:

s1 = "Welcome " 
s2 = "to "
s3 = "Sling Academy!"

concatenated_string = s1 + s2 + s3
print(concatenated_string)

Output:

Welcome to Sling Academy!

Using f-strings (Python 3.6+)

f-strings allow you to embed expressions inside curly braces to dynamically format and concatenate strings.

Example:

name = "Demon Slayer"
age = 99
location = "The Underworld"

concatenated_string = f"Hello, my name is {name} and I am {age} years old. I live in {location}."

print(concatenated_string)

Output:

Hello, my name is Demon Slayer and I am 99 years old. I live in The Underworld.

Using the str.format() method

You can concatenate strings by using the str.format() method, which formats and inserts values into a string template.

Example:

string1 = "The robot"
string2 = "said 'Hello World'"
string3 = "then he waved his hands"

concatenated_string = str.format("{0} {1}, {2}.", string1, string2, string3)
print(concatenated_string)

Output:

The robot said 'Hello World', then he waved his hands.

This technique used to be used a lot in the old days before the arrival of f-strings.

Using the join() method

You can unify a large number of strings by using the join() method with a specified delimiter.

Example:

strings = [
    "Elden Ring",
    "and",
    "God of War",
    "are",
    "both",
    "great",
    "games."
]

concatenated_string = " ".join(strings)
print(concatenated_string)

Output:

Elden Ring and God of War are both great games.

Using the % operator

This is a string formatting operator that can also be used for string concatenation. It replaces the placeholders in a format string with the values specified after the operator. It can be useful when we want to concatenate strings and perform simple formatting.

Example:

str1 = "Sling"
str2 = "Academy"
str3 = "%s %s" % (str1, str2)
print(str3)

Output:

Sling Academy

Using the += operator

This is an augmented assignment operator that adds the right operand to the left operand and assigns the result to the left operand. It can be used to append one string to another.

Example:

str1 = "Sling"
str2 = "Academy"
str1 += " " + str2
print(str1)

Output:

Sling Academy

Using the print() function with commas

You can use the print() function with commas to print multiple strings at once. The output is a string generated by concatenating input strings.

Example:

s1 = "One"
s2 = "Two"
s3 = "Three"
s4 = "Four"
print(s1, s2, s3, s4)

Output:

One Two Three Four

By default, the separator is a whitespace character. However, you can change it if you want, like this:

s1 = "One"
s2 = "Two"
s3 = "Three"
s4 = "Four"

print(s1, s2, s3, s4, sep=" - ")

Output:

One - Two - Three - Four

Concatenating literal strings

You can concatenate literal strings just by putting them one after another, and Python will do the job for you. However, this technique doesn’t work with variables.

Example:

s = "I don't know " "what is the difference between " "Coke and Pepsi."
print(s)

Output:

I don't know what is the difference between Coke and Pepsi.

The example above might mislead you into thinking that this method isn’t very useful and hard to read (because we put everything on the same line, with so many quotes). However, with a pair of parentheses, things are much better:

complex_string = (
    'This is a very long string that '
    'spans multiple lines and '
    'continues on this line. '
    'No, it is not over yet. '
    'By the way, happy coding with Sling Academy.'
)

print(complex_string)

Output:

This is a very long string that spans multiple lines and continues on this line. No, it is not over yet. By the way, happy coding with Sling Academy.

Next Article: Python: Converting a string to lowercase/uppercase

Previous Article: Python: 5 Ways to Reverse a String

Series: Working with Strings in Python

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots