Python

Python String Concatenation Examples

In computing, string combining is an overall common process. Concatenating strings in Python may be expressed in a variety of ways. This tutorial will look at various methods for concatenating strings inside a Python application. To concatenate strings with a delimiter, we may use the join() method. It is beneficial to have a character sequence, such as a list or perhaps a tuple of characters. Then, use the join() method with an empty string whenever you don’t want a delimiter. Use these methods according to your needs. Whenever concatenation requires any formatting, just use format() as well as f-string functions. It’s worth noting that f-string only works with Python 3.6 and higher. Let’s have a look at each one of them.

Example 01: Concatenate With “+” Operator

Log in from the Ubuntu login panel and open the terminal from the applications via “Ctrl+Alt+T”.  After opening it, let’s create a new python file “one.py” via the “touch” query as follows:

$ touch one.py

The file has been created. Open it from the file explorer by navigating to the “Home” directory. Double-tap to open the file and write out the following code in your newly created file. This code contains two string-type variables v1 and v2, with some value in both of them. The variable “name” has been initialized to concatenate both the variables v1 and v2 using the “+” operator within them. After that, the print statement has been used to print the concatenated variable “name”. Save your file with “Ctrl+S” and quit it.

Let’s execute the file by using the “python3” query in the shell below. You will see that it will show a concatenated string e.g., “Aqsa Yasin”, made from two string variables:

$ python3 one.py

The “+” sign acts differently for integer-type variables. This operator sums up the integers instead of concatenating them. For example, let’s update the value of both variables v1 and  v2 with integers and use the “+” sign to merge them. When we print the resultant value, it shows the sum of both variables instead of the concatenated value. Save the file with the “Ctrl+S” shortcut and leave it:

Upon execution, you can see that the output is an integer sum-up value:

$ python3 one.py

Let’s use one integer type and one string type variable in the concatenation example. Update the code with the following script having two variables e.g., string and integer. Use the “+” operator to concatenate both and print them:

After saving our file with “Ctrl+S”, we will close our file and navigate to the terminal again. Execution of this code will be done by the stated command below. Unfortunately, the output for this code will lead you to an error because the “+” sign doesn’t work on two different kinds of data types to concatenate them.

$ python3 one.py

Example 02: Concatenate With Join()

It’s time to have a look at a different example for concatenation. We will be using the join() function to concatenate two strings. Update the code as shown below. We have two string-type variables v1 and v2, defined in the code. We have concatenated both variables with the join function. Upon passing into its parameter, they become concatenated and then printed out:

Save the file and open your terminal. Execute the code with the “python3” query as below. You will see that it shows the concatenated result “Aqsa Yasin” of variables v1 and v2 using the join method:

$ python3 one.py

Example 03: Concatenate With “%” Operator

Let’s have another example of concatenation. This time, we will be utilizing the percentage operator in our code to do so. We have taken two string-type variables v1 and v2, with different values. After that, we have created another variable, “new” and defined a percentage format along with the sign “%”. We have also given both the variables in their parameters. At last, we have printed this resultant value of string concatenated by the percentage operator in a print statement. Save your file and click on the cross sign to close it:

Upon execution, you can see it working properly and showing the concatenated new string from both two variables using a percentage operator:

$ python3 one.py

Example 04: Concatenate With Format Function

Now, we will be using another function to concatenate two new variables v1 and v2. We defined a format for this function in the newly created variable “new” and passed both the variables v1 and v2 in its parameters. Last, we have given this freshly concatenated variable “new” in the print statement to be printed out shortly.

After saving and closing the file, let’s begin with the execution of our updated code. We have been using the very same instruction in the shell for execution. The output for the execution of this code shows the concatenated value “Linux-Python” of both the variables that have been saved into the variable “new”.

$ python3 one.py

Example 05: Concatenate With F-string

The last and the unique example for concatenating two string-type variables is via f-string.  We have initialized two new variables v1 and v2, with string type values in both of them. After this, we have initialized another variable, “new”, and defined f-string type format in it with variables v1 and v2 within its format. In the last line of code, we have used the print statement in which we have passed the concatenated variable “new” to print it in concatenated format.

Save your file and close it once again. Now open the terminal and execute the code file via the “python3” keyword along with the name of a file “one.py”. The output presents the concatenated value of variable “new” while using the f-string format of concatenation.

$ python3 one.py

Conclusion:

We have learned five methods to concatenate the strings in our examples using this tutorial guide. I hope this article will better demonstrate the process of Python String Concatenation

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.