Table of contents
1.
Introduction
2.
What is String Concatenation in Python?
3.
Different Ways of String Concatenation in Python
3.1.
Method 1: Python String Concatenation using + Operator
3.2.
Python
3.3.
Method 2: Python String Concatenation using join() Method
3.4.
Python
3.5.
Method 3: Python String Concatenation using % Operator
3.6.
Python
3.7.
Method 4: Python String Concatenation using format() function 
3.8.
Python
3.9.
Method 5: Python String Concatenation using (, comma)
3.10.
Python
3.11.
Method 6: Python String Concatenation using f-string
3.12.
Python
4.
Frequently Asked Questions
4.1.
What is the most efficient way to concatenate strings in Python?
4.2.
How to concatenate two strings?
4.3.
What is concat() in Python?
4.4.
Can we concatenate three strings in Python?
5.
Conclusion
Last Updated: Nov 11, 2024
Easy

Python String Concatenation

Author Tisha
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python, string concatenation is a common operation that allows you to combine multiple strings into a single one. Whether you’re building dynamic text for user interfaces, processing input data, or generating reports, string concatenation is essential. Python offers various ways to concatenate strings, each with its own use cases and benefits. From using the + operator to more efficient methods like join(), understanding the different techniques can help you write cleaner, more performant code. In this blog, we will explore the different ways to concatenate strings in Python.

Python String Concatenation

What is String Concatenation in Python?

Concatenating the two strings means merging both strings. Concatenation of "Coding" and "Ninjas" will result in "Coding Ninjas".

Here, we will discuss the different methods with which we can do String Concatenation in Python. Python String Concatenation can be done using the below ways. 

  • Using + operator. 
     
  • Using join() method. 
     
  • Using % operator. 
     
  • Using format() function. 
     
  • Using ',' comma.
     
  • Using f-string (Literal String Interpolation).

Different Ways of String Concatenation in Python

Method 1: Python String Concatenation using + Operator

The simplest Method is to use the + Operator for the Python string Concatenation. This Operator is used to add multiple strings together. Here, the arguments should be a string. The + Operator joins the string stored in v1 and v2 and stores it in another variable, v3.

  • Python

Python

# Defining the strings
v1 = "Coding "
v2 = "Ninjas"
# The + Operator used to combine the strings
v3 = v1 + v2
print(v3)
You can also try this code with Online Python Compiler
Run Code


Output:

Coding Ninjas


Method 2: Python String Concatenation using join() Method

The join() method returns a string with a str separator joining the sequence elements. This Method will combine the strings stored in the v1 and v2 variables. This Method only accepts the list as the argument; the list size can be anything. 

  • Python

Python

v1 = "Coding"
v2 = "Ninjas"
# join() method used to combine strings
print("".join([v1, v2]))
# join() method is used to combine
# string with the separator Space(" ")
v3 = " ".join([v1, v2])
print(v3)
You can also try this code with Online Python Compiler
Run Code


Output:

CodingNinjas
Coding Ninjas


Method 3: Python String Concatenation using % Operator

The % operator is used for string formatting. It can be used for string concatenation also. This Operator is useful when we need to concatenate the strings and perform simple formatting. The % denotes string data type. The values in both the variables will be passed to the string %s and become "Coding Ninjas".

  • Python

Python

v1 = "Coding"
v2 = "Ninjas"

# % Operator used to combine string
print("% s % s" % (v1, v2))
You can also try this code with Online Python Compiler
Run Code


Output:

Coding Ninjas


Method 4: Python String Concatenation using format() function 

This Method allows value formatting and multiple substitutions in Python. This Method is used in Python for string formatting as string.format(). This Method concatenates the elements within a string through positional formatting. In this Method, curly braces {} will be used to set the position of the strings. The first curly brace stores the first variable, and the second curly brace stores the second variable. Finally, it will print  "Coding Ninjas".

  • Python

Python

v1 = "Coding"
v2 = "Ninjas"
print("{} {}".format(v1,v2))

# store the output in another variable
v3 = "{} {}".format(v1, v2)
print(v3)
You can also try this code with Online Python Compiler
Run Code


Output:

Coding Ninjas
Coding Ninjas


Method 5: Python String Concatenation using (, comma)

When you need to include only one whitespace, one of the best alternatives to string concatenation using the “+” operator is “,”

  • Python

Python

v1 = "Coding"
v2 = "Ninjas"

# combine data types with the single whitespace.
print(v1, v2)
You can also try this code with Online Python Compiler
Run Code


Output:

Coding Ninjas


Method 6: Python String Concatenation using f-string

If you use Python 3.6+, you can easily use the f-string function for string concatenation. This new method to format the strings was introduced in PEP 498 - Literal String Interpolation.

  • Python

Python

v1 = 'Coding'
v2 = 'Ninjas'
v3 = f'{v1} {v2}'
print('String Concatenation =', v3)
You can also try this code with Online Python Compiler
Run Code


Output:

String Concatenation = Coding Ninjas


You can practice by yourself with the help of online python compiler for better understanding.

Frequently Asked Questions

What is the most efficient way to concatenate strings in Python?

Use the join() method in Python for efficient string concatenation; it creates one string object. In contrast, the + operator generates a new object each time it's used.

How to concatenate two strings?

Using the + operator or the str.join() method, you can concatenate two strings in Python.

What is concat() in Python?

Python does not have a concat() method for strings. String concatenation is typically done using the + operator or join() method.

Can we concatenate three strings in Python?

Yes, you can concatenate three strings in Python using the + operator, join() method, or other string manipulation techniques.

Conclusion

In this article, we have learned Python String Concatenation. Python offers several methods for string concatenation, each with its own advantages depending on the use case. While the + operator is simple and intuitive, it may not always be the most efficient for concatenating multiple strings, especially in loops. Methods like join() provide better performance and are ideal for concatenating large numbers of strings.

You can also refer to the below articles:

You can learn the basics of Java and data structures and algorithms in Java on Code360. Refer to our guided path on code studio to learn Competitive Programming, Javascript System Design, etc. Enroll in our courses and refer to the mock test and problems available. Also, look at the interview experiences for placement preparations.

Live masterclass