Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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 are the different types of concatenation?
4.4.
How does Python handle string concatenation?
4.5.
How many strings can you concatenate in Python?
4.6.
What symbol is used for concatenation in Python?
4.7.
Why avoid string concatenation?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Python String Concatenation

Author Tisha
0 upvote
gp-icon
Basics of python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

In this article, we will learn how to concatenate strings in Python. We will also see the different methods and their syntax and implementation. We will also look at some examples of Python string Concatenation. 

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).
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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)


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)


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))


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)


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)


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)


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 are the different types of concatenation?

There are two types of concatenation: Partitioned concatenation and Sequential concatenation.

How does Python handle string concatenation?

Using the '+' Operator, 'join()' method, '%' operator, 'f-string' and using the 'format()' function. 'f-strings' was introduced in Python 3.6 only. It provides a concise way to embed expressions inside string literals.

How many strings can you concatenate in Python?

Concatenate strings in Python by using the + operator for different strings and the * operator for repeating the same string.

What symbol is used for concatenation in Python?

The + operator is used to concatenate strings in Python. To combine two or more strings into one new string that comprises the contents of the concatenated strings, you can just use the + operator. For instance, the phrase "Hello, world!" would be produced by adding "Hello, " and "world!".

Why avoid string concatenation?

String concatenation is generally avoided in Python for quite a few reasons. In Python, string concatenation can be inefficient, particularly while concatenating many strings. Excessive string concatenation can make code harder to read and understand, especially when long concatenation chains are involved. 

Conclusion

In this article, we have learned how to concatenate strings in Python. We have also seen the different functions and methods with which we can do Python string Concatenation. We have also seen the programming and some examples of it. 

You can also refer to the below articles:

You can learn the basics of Java and data structures and algorithms in Java on Coding Ninjas. 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.

Happy Learning, Ninjas.

Previous article
Python String replace() Method
Next article
Capitalize() in Python
Guided path
Free
gridgp-icon
Basics of python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass