Python String Concatenation
Last Updated :
05 Jan, 2024
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:
- Using + operator
- Using join() method
- Using % operator
- Using format() function
- Using “,” (comma)
- 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
var1 = "Hello "
var2 = "Geek"
var3 = var1 + var2
print (var3)
|
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"
print ("".join([var1, var2]))
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"
print ( "% s % s" % (var1, var2))
|
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"
print ( "{} {}" . format (var1, var2))
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"
print (var1, var2, var3)
|
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
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:
Please Login to comment...