Open In App

Python String Concatenation

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:



Last Updated : 05 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads