String Concatenation in Python
Overview
String Concatenation in Python refers to combining two or more strings to form a new string. Several methods are used for the concatenation of two or more strings.
Introduction to String Concatenation in Python
Have you ever wondered how you would join/combine the first name of a person with his/her last name together to get that person's full name? String Concatenation is the way to combine those types of strings easily. Read along to know more.
What is String Concatenation in Python?
String concatenation is the method of combining two or more strings.
As we know, strings are immutable. So concatenation techniques don't update the original string but create a new string and store it in the new memory location. We generally get a completely new string after concatenating two or more strings.
There are several ways that are used for String Concatenation in Python. These are:
- Using the + operator
- Using the * operator
- Using the % operator
- Using ,
- Using join() method
- Using format() function
Each method is explained below with the help of complete syntax and an example. Read along to know more.
Example of String Concatenation in Python
Suppose you must fill out an online form for registration into an annual function. In the form, you have three fields mentioning: Title, First Name , and Last Name. How do you think that the host will club all the data to get your complete name with the title?
The concept of String Concatenation is used here as you have studied different ways to concatenate the strings earlier. Now use some ways to implement this scenario to get the full name.
Code:
Output:
Different Ways of String Concatenation in Python
Using + operator
'+' is the best and the easiest way to concatenation of 2 or more strings. It doesn't provide blank spaces within the concatenated strings by default.
Syntax:
Code:
Output:
Explanation:
- We created two variables for storing different strings, which need to be concatenated.
- Then, we use the + operator to concatenate strings.
- After that, we got the output where there is no space between concatenated strings.
Using * operator
The repetition operator * will make multiple copies of that particular object and combines them together. This method concatenates the same string again and again with itself. This will, in return, give us a repetition of the string, and finally, we get a concatenated string as an output.
It doesn't provide blank spaces within the concatenated strings by default.
Syntax:
Code:
Output:
Explanation:
- First of all, we created a variable for storing the value of the string, which we have to concatenate.
- Then, we use the * operator and a number representing the number of times the string should repeat itself.
- After that, we got the output where the string is concatenated n times, and there is no space in between of concatenated string
Using % operator
% operator is used basically for formatting purposes. But it can also be used for string concatenation. The % Operator combines the string stored in the str1 and str2. The %s denotes string data type.
It doesn't provide blank spaces within the concatenated strings by default.
Syntax:
Code:
Output:
Explanation:
-
We created two variables for storing different strings, which need to be concatenated.
-
Then, we use the % operator to concatenate strings.
-
'%s' denotes the values which correspond to string.
-
After that, we got the output where there is no space between concatenated strings.
Using join() method
The Join() method is used for string concatenation in Python by changing the array to a string. This method takes an array as a parameter and returns the concatenated string as an output.
It provides a single blank space within the concatenated strings by default.
Syntax:
Code:
Output
Explanation:
- We created two variables for storing different strings which need to be concatenated.
- Then, we use .join() method for the concatenation of strings.
- As it takes an array of strings for the concatenation process. So, we passed an array of string to it.
- After that, we got the output of concatenated strings where we got a single white space between 2 words.
Using format() function
The format() method formats the specified value(s) and insert them inside the string's placeholder. We can format() for string concatenation as well. The placeholder is defined using curly brackets {}.
It provides blank spaces within the concatenated strings by default.
Syntax:
Code:
Output:
Explanation:
- We created two variables for storing different strings which need to be concatenated.
- Then, we use .format() function for concatenation of strings. As it takes arguments of strings for the concatenation process. So, we passed two strings as an argument to it.
- When we use the {} operators with the format() function, they serve as placeholders for the variables you would like to store inside a string.
- After that, we got the output of concatenated strings where there is white space between two concatenated strings.
Using ,
The , is just an alternative for the '+' operator. It is used when we want to give blank spaces within our concatenated string.
Syntax:
Code:
Output:
Explanation:
- We created two variables for storing different strings which need to be concatenated.
- Then, we use the , operator to concatenate strings.
- After that, we got the output where there is a single white space between concatenated strings.
Conclusion
- First of all, we learned string concatenation in Python and what is the basic use of string concatenation in our day-to-day life.
- Then, we understood different ways by which we can concatenate different strings.
- We learned the syntax of each method and dry-ran each available method for string concatenation.
- String concatenation is used for joining first and last names together to get the person's full name.