How to concatenate two strings in Python

In Python, you can concatenate two strings using the + operator. Here’s an example:

string1 = "Hello"
string2 = "World"
result = string1 + " " + string2
print(result)

Output:

Hello World

In the above example, we first defined two strings string1 and string2. We then concatenated them using the + operator and a space character to get a new string result, which we printed to the console.

Using join() method:

Yes, you can also concatenate two strings using the join() method in Python. Here’s an example:

string1 = "Hello"
string2 = "World"
result = " ".join([string1, string2])
print(result)

Output:

Hello World

In the above example, we first defined two strings string1 and string2. We then used the join() method to concatenate the two strings with a space character in between. The join() method takes a list of strings as an argument, where each string in the list will be joined together using the delimiter specified in the join() method. Finally, we printed the result to the console.

Note that in this example, we created a list containing string1 and string2 before passing it to the join() method. Alternatively, you can also pass the two strings directly to the join() method like this:

string1 = "Hello"
string2 = "World"
result = " ".join(string1, string2)
print(result)

Output:

Hello World

In this case, we passed string1 and string2 directly to the join() method as separate arguments instead of as a list.

Using % Operator:

Yes, you can also concatenate two strings using the % operator in Python. Here’s an example:

string1 = "Hello"
string2 = "World"
result = "%s %s" % (string1, string2)
print(result)

Output:

Hello World

n the above example, we first defined two strings string1 and string2. We then used the % operator to concatenate the two strings with a space character in between. The % operator is used to substitute values into a string. In this case, we used the %s placeholder to indicate that we want to substitute a string value into the string. The values to substitute are passed as a tuple (string1, string2) after the % operator. Finally, we printed the result to the console.

Note that the % operator can be used to substitute values of different types into a string, not just strings. For example, to concatenate a string and a number, you can use the %d placeholder for integers or the %f placeholder for floating-point numbers. Here’s an example:

string1 = "The value of pi is"
pi = 3.14159
result = "%s %.2f" % (string1, pi)
print(result)

Output:

The value of pi is 3.14

In this example, we used the %f placeholder to substitute a floating-point number into the string. The .2 after the % sign specifies that we want to round the number to two decimal places.

Using format() function:

Yes, you can also concatenate two strings using the format() function in Python. Here’s an example:

string1 = "Hello"
string2 = "World"
result = "{} {}".format(string1, string2)
print(result)

Output:

Hello World

In the above example, we first defined two strings string1 and string2. We then used the format() function to concatenate the two strings with a space character in between. The format() function takes one or more arguments and returns a formatted string. In this case, we used empty curly braces {} as placeholders to indicate where we want to insert the string values. The values to insert are passed as arguments to the format() function in the order in which they appear in the string. Finally, we printed the result to the console.

Note that you can also use numbered placeholders {0}, {1}, etc. to specify the order of the arguments passed to the format() function. For example:

string1 = "Hello"
string2 = "World"
result = "{0} {1}".format(string1, string2)
print(result)

Output:

Hello World

In this example, we used numbered placeholders {0} and {1} to specify that the first argument passed to the format() function should be inserted into the first placeholder, and the second argument should be inserted into the second placeholder.