How do I append one string to another in Python?
You can use the "+" operator to concatenate two strings in Python. Here's an example:
string1 = "Hello"
string2 = "world"
result = string1 + " " + string2
print(result) # Output: "Hello world"
Watch a video course
Python - The Practical Guide
You can also use the +=
operator to append one string to another:
string1 = "Hello"
string2 = "world"
string1 += " " + string2
print(string1) # Output: "Hello world"
or the join()
method of the first string, passing the second string as an argument:
string1 = "Hello"
string2 = "world"
result = " ".join([string1, string2])
print(result) # Output: "Hello world"
Note that join()
method expects a list of strings, so you have to pass a list containing the two strings you want to join.