How to Concatenate Two Strings in Python?

Estimated read time 1 min read

In Python, you can concatenate two strings using the + operator or by using the str.join() method. Here’s an example of both methods:

Method 1: Using the + operator

string1 = 'Hello, '
string2 = 'world!'
concatenated = string1 + string2
print(concatenated)

Method 2: Using the str.join() method

string1 = 'Hello, '
string2 = 'world!'
concatenated = ''.join([string1, string2])
print(concatenated)

Both methods will give you the same result, which is the concatenated string. The + operator concatenates the two strings directly, while the str.join() method joins the strings from a list. In this case, we create a list [string1, string2] and join them using an empty string '' as the separator.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply