Concatenation is the process of combining two or more strings to form a new string. In Python 3.10, you can concatenate strings using the "+" operator or the f-string format.
Here are some examples:
1. Using the "+" operator:
string1 = "Hello"
string2 = "world"
result = string1 + " " + string2
print(result) # Output: "Hello world"
2. Using f-string format:
string1 = "Hello"
string2 = "world"
result = f"{string1} {string2}"
print(result) # Output: "Hello world"
You can also concatenate strings in a loop:
names = ["Alice", "Bob", "Charlie"]
greeting = "Hello, "
for name in names:
message = greeting + name
print(message)
Output:
Hello, Alice
Hello, Bob
Hello, Charlie
Note that you can also concatenate strings using the * operator to repeat a string multiple times:
word = "spam"
result = word * 3
print(result) # Output: "spamspamspam"
It is important to keep in mind that when you concatenate strings, you are creating a new object in memory, which can add up if you are working with a large number of strings. In some cases, it may be more efficient to use other methods such as string formatting or join() method.