The Wayback Machine - https://web.archive.org/web/20240718005000/https://www.geeksforgeeks.org/python-string-concatenation/
Open In App

Python String Concatenation

Last Updated : 05 Jan, 2024
Improve
Suggest changes
Post a comment
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

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
String slicing in Python to check if a string can become empty by recursive deletion
Given a string “str” and another string “sub_str”. We are allowed to delete “sub_str” from “str” any number of times. It is also given that the “sub_str” appears only once at a time. The task is to find if “str” can become empty by removing “sub_str” again and again. Examples: Input : str = "GEEGEEKSKS", sub_str = "GEEKS" Output : Yes Explanation :
2 min read
String slicing in Python to Rotate a String
Given a string of size n, write functions to perform following operations on string. Left (Or anticlockwise) rotate the given string by d elements (where d <= n).Right (Or clockwise) rotate the given string by d elements (where d <= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeek
3 min read
Python | Sorting string using order defined by another string
Given two strings (of lowercase letters), a pattern and a string. The task is to sort string according to the order defined by pattern and return the reverse of it. It may be assumed that pattern has all characters of the string and all characters in pattern appear only once. Examples: Input : pat = "asbcklfdmegnot", str = "eksge" Output : str = "g
2 min read
String Alignment in Python f-string
Text Alignment in Python is useful for printing out clean formatted output. Some times the data to be printed varies in length which makes it look messy when printed. By using String Alignment the output string can be aligned by defining the alignment as left, right or center and also defining space (width) to reserve for the string. Approach : We
2 min read
String to Int and Int to String in Python
Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting a string to int and int to string. Converting a string to an int If we want to convert a number that is represented in the string to int, we have to use the int() function. This function is used
2 min read
Pad or fill a string by a variable in Python using f-string
f-string stands for formatted string. It had come up by Python Version 3.6 and rapidly used to do easy formatting on strings. F-string is a string literal having syntax starts with f and followed by {}. That placeholder used for holding variable, that will be changed upon the variable names and their values respectively. There are already strings f
4 min read
Python string length | len() function to find string length
The string len() function returns the length of the string. In this article, we will see how to find the length of a string using the string len() method. Example: C/C++ Code string = "Geeksforgeeks" print(len(string)) Output13String len() Syntax len(string) Parameter String: string of which you want to find the length. Return: It returns
3 min read
Python String Formatting - How to format String?
String formatting allows you to create dynamic strings by combining variables and values. In this article, we will discuss about 5 ways to format a string. You will learn different methods of string formatting with examples for better understanding. Let's look at them now! How to Format Strings in PythonThere are five different ways to perform stri
8 min read
Practice Tags :
three90RightbarBannerImg