Javatpoint Logo
Javatpoint Logo

How to concatenate two strings in Python

Introduction:

In this article, we are discussing how to concatenate two strings in Python. Here we mainly add two strings and form a single string. Python string is a collection of Unicode characters. Python provides many built-in functions for string manipulation. String concatenation is a process in which one string is merged with another. So, the concatenation of two strings is done by some process, these are -

  1. Using + operators
  2. Using join() method
  3. Using % method
  4. Using format() function

Now we briefly discuss these concatenation methods in Python.

1. Using + Operator:

This is an easy way to combine the two strings. The + operator adds the multiple strings together. Strings must be assigned to the different variables because strings are immutable. Let's understand the following example.

Program Code:

Here we give an example of concatenation in Python. First, we initialized two strings and added them with the "+" Operator. Then store the value in another variable and then print it. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

The new combined string is: Hello Coders

Explanation:

In the above example, the variable str1 stores the string "Hello", and variable str2 stores the "Coders". We used the + operator to combine these two string variables and stored in str3. Then we print the new combined string str3.

2. Using join() method

The join() method is used to join the string in which the str separator has joined the sequence elements. Let's understand the following example.

Program Code:

Here we give an example of concatenation in Python. First, we initialized two strings and then added them using the join() method. Then store the value in another variable and then print it. The code is given below -

Output:

HelloJavaTpoint
The new combined string is: Hello JavaTpoint

Explanation:

In the above code, the variable str1 stores the string "Hello" and variable str2 stores the "JavaTpoint". The join() method returns the combined string that is stored in the str1 and str2. The join() method takes only list as an argument. After using of join() method, we store the combined string in another variable, str3. Then we print it.

3. Using % Operator

The % operator is used for string formatting. It can also be used for string concatenation. Let's understand the following example.

Program Code:

Here we give an example of concatenation in Python. First, we initialized three strings and added them with the "%" operator. Then we print the concatenation of three strings. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Hello coders India

Explanation -

In the above code, the %s represents the string data-type. We passed every variables value to the %s that combined the strings and returned the " Hello coders India".

4. Using format() function

Python provides the str.format() function, which allows use of multiple substitutions and value formatting. It accepts the positional arguments and concatenates the string through positional formatting. Let's understand the following example.

Program Code:

Here we give an example of concatenation in Python. Firstly, we take user input of three strings and then add them by format() function. Then store the value in another variable and then print it. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Enter the value of Str1: Welcome 
Enter the value of Str2: To
Enter the value of Str3: JavaTpoint
Welcome To JavaTpoint
Welcome To JavaTpoint

Explanation:

In the above code, the format() function combines the three strings taken as user inputs and stores the combined string into the str4 variable. The curly braces {} are used as the position of the strings. Then we print the str4.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA