Open In App

Python String Concatenation

Last Updated : 05 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

String concatenation is the process of joining two or more strings together and forming one single string. You can use the + operator, or use a built-in function like str.join() to concatenate two strings.

In Python, Strings are arrays of bytes representing Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of “1”.

Square brackets [ ] can be used to access elements of the string. In this tutorial, we will learn how to concatenate strings in Python with examples and Python programs.

Example

Input:  string1, string2
Output: "string1 string2"
Explanation: This example shows string1 and string 2 being concatenated and
forming a new string together.

How to Concatenate Strings in Python

Python String Concatenation is the technique of combining two strings. Let’s look at different ways to concatenate strings:

  1. Using + operator 
  2. Using join() method 
  3. Using % operator 
  4. Using format() function 
  5. Using “,” (comma)
  6. Using f-string ((Literal String Interpolation))

1. String Concatenation using ‘+’ Operator

It’s very easy to use the + operator for string concatenation. This operator can be used to add multiple strings together. However, the arguments must be a string, if used on integers it performs mathematical addition. 

Note: Strings are immutable, therefore, we don’t modify the string instead we concatenate them together and assign them to a new variable.

Example: Here, The + Operator combines the string that is stored in the var1 and var2 and stores in another variable var3.

Python3




# Defining strings
var1 = "Hello "
var2 = "Geek"
 
# + Operator is used to combine strings
var3 = var1 + var2
print(var3)


Output

Hello Geek

2. String Concatenation using join() Method

The join() method is a string method and returns a string in which the elements of the sequence have been joined by a string separator.

It accepts only the list as its argument and list size can be anything. Here, we will see about Python concatenate string done by using join().

Example: This method combines the string that is stored in var1 and var2 using join() method and stores it in var3.

Python3




var1 = "Geeks"
var2 = "forGeeks"
 
# join() method is used to combine the strings
print("".join([var1, var2]))
 
# join() method is used here to combine
# the string with a separator Space(" ")
var3 = " ".join([var1, var2])
 
print(var3)


Output

GeeksforGeeks
Geeks forGeeks

3. String Concatenation using ‘%’ Operator

We can use the % operator for string formatting, it can also be used for string concatenation. It’s useful when we want to concatenate strings and perform simple formatting. 

Example: Here, we will see about Python concatenate string done by using the % operator.

Python3




var1 = "Welcome"
var2 = "Geek"
 
# % Operator is used here to combine the string
print("% s % s" % (var1, var2))


Output

Welcome Geek

Explanation: The %s denotes string data type. The value in both the variable is passed to the string %s and becomes “Welcome Geek”.

4. String Concatenation using format() function

The str.format() is one of the string formatting methods in Python, which allows multiple substitutions and value formatting. It concatenates elements within a string through positional formatting. 

The curly braces {} are used to set the position of strings.

Example:

Python3




var1 = "Hello"
var2 = "Geeks"
 
# format function is used here to
# combine the string
print("{} {}".format(var1, var2))
 
# store the result in another variable
var3 = "{} {}".format(var1, var2)
 
print(var3)


Output

Hello Geeks
Hello Geeks

Explanation: The first variable stores in the first curly braces and the second variable stores in the second curly braces. Finally, it prints the value “Hello Geeks”.

5. String Concatenation using “, ” comma

A comma “,” is a great alternative to string concatenation using “+”. when you want to include a single whitespace. Use a comma when you want to combine data types with single whitespace in between.

Python3




var1 = "Geeks"
var2 = "for"
var3 = "Geeks"
 
# using comma to combine data types
# with a single whitespace.
print(var1, var2, var3)


Output

Geeks for Geeks

6. String Concatenation using f-string

Using F-string for string concatenation can only be done on Python versions above 3.6, it was introduced in introduced in PEP 498 – Literal String Interpolation.

In this example, the below code initializes the variables `name` with “John” and `age` with 25, then creates a greeting using an f-string that incorporates these variables, finally printing the greeting.

Example:

Python3




name = "GFG"
age = 25
 
# String concatenation using f-string
greeting = f"Hello, my name is {name} and I am {age} years old."
 
print(greeting)


Output

Hello, my name is GFG and I am 25 years old.

In this article, we have covered 6 ways to concatenate strings in Python. Python functions like join(), and format() can be used for the task, or you can also join Python operators like ” + operator” and “% operator”. Using the latest techniques like f-strings is also explained in this tutorial.

String concatenation is an important string operation, and every Python programmer should know it. Some more articles on Python string concatenation are mentioned below:



Previous Article
Next Article

Similar Reads

Python - Concatenation of two String Tuples
Sometimes, while working with records, we can have a problem in which we may need to perform String concatenation of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. Method #1 : Using zip() + generator expression The combination of above functions can be used to perform this task
3 min read
Python - String concatenation in Heterogeneous list
Sometimes, while working with Python, we can come across a problem in which we require to find the concatenation of strings. This problem is easier to solve. But this can get complex cases we have a mixture of data types to go along with it. Let’s discuss certain ways in which this task can be performed. Method #1 : Using loop + conditions We can e
4 min read
Python - String Matrix Concatenation
Sometimes, while working with Matrix we can have a problem in which we have Strings and we need a universal concatenation of all the String present in it. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + join() We can solve this problem using list comprehension as a potential shorthand to the co
4 min read
Python - Triple quote String concatenation
Sometimes, while working with Python Strings, we can have a problem in which we need to perform concatenation of Strings which are constructed by Triple quotes. This happens in cases we have multiline strings. This can have applications in many domains. Let us discuss certain ways in which this task can be performed. Input : test_str1 = """mango is
4 min read
Python - Incremental Slice concatenation in String list
Given a String list, perform the task of the concatenation of strings by increasing the size of each string. Examples: Input : test_list = ['gfg', 'for', 'all', 'geeks'], Output : gfoallgeek Explanation : g, fo, all, geek -> concatenated from each string [ increasing order ].Input : test_list = ['gfg', 'for', 'geeks'], Output : gfogee Explanatio
4 min read
String concatenation in Scala
A string is a sequence of characters. In Scala, objects of String are immutable which means a constant and cannot be changed once created. when a new string is created by adding two strings is known as a concatenation of strings. Scala provides concat() method to concatenate two strings, this method returns a new string which is created using two s
2 min read
Element-wise concatenation of two NumPy arrays of string
In this article, we will discuss how to concatenate element-wise two arrays of string Example : Input : A = ['Akash', 'Ayush', 'Diksha', 'Radhika'] B = ['Kumar', 'Sharma', 'Tewari', 'Pegowal'] Output : A + B = [AkashKumar, AyushSharma, 'DikshTewari', 'RadhikaPegowal'] We will be using the numpy.char.add() method. Syntax : numpy.char.add(x1, x2)Para
2 min read
Python - Alternate Strings Concatenation
The problem of getting the concatenation of a list is quite generic and we might someday face the issue of getting the concatenation of alternate elements and get the list of 2 elements containing the concatenation of alternate elements. Let’s discuss certain ways in which this can be performed. Method #1: Using list comprehension + list slicing +
3 min read
Python | Records List Concatenation
Sometimes, while working with Python records, we can have a problem in which we need to perform cross concatenation of list of tuples. This kind of application is popular in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + zip() The combination of above functionalities ca
4 min read
Python | Consecutive prefix overlap concatenation
Sometimes, while working with Python Strings, we can have application in which we need to perform the concatenation of all elements in String list. This can be tricky in cases we need to overlap suffix of current element with prefix of next in case of a match. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop + e
5 min read
Practice Tags :