In Python, you can concatenate strings using the + operator or the str.join() method. Here's how you can do it:
Using the+
Operator:
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # Output: Hello World
Using the +=
Operator:
str1 = "Hello"
str2 = "World"
str1 += " " + str2
print(str1) # Output: Hello World
Using f-strings (Python 3.6 and above):
str1 = "Hello"
str2 = "World"
result = f"{str1} {str2}"
print(result) # Output: Hello World
Using .join()
method
strings = ["Hello", "World"]
result = " ".join(strings)
print(result) # Output: Hello World
Using .format()
method (Python 3.x):
str1 = "Hello"
str2 = "World"
result = "{} {}".format(str1, str2)
print(result) # Output: Hello World
Using %
Operator (deprecated in Python 3.x):
str1 = "Hello"
str2 = "World"
result = "%s %s" % (str1, str2)
print(result) # Output: Hello World
Choose the method that best suits your coding style and the requirements of your program.