在Python中,字符串的拼接可以使用以下幾種方法:
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # 輸出:Hello World
str_list = ["Hello", "World"]
result = " ".join(str_list)
print(result) # 輸出:Hello World
name = "Alice"
age = 25
result = f"My name is {name} and I am {age} years old."
print(result) # 輸出:My name is Alice and I am 25 years old.
name = "Alice"
age = 25
result = "My name is {} and I am {} years old.".format(name, age)
print(result) # 輸出:My name is Alice and I am 25 years old.
str1 = "Hello"
str2 = "World"
result = str1[:2] + str2[2:]
print(result) # 輸出:Horld
以上是幾種常見的字符串拼接方法,選擇適合你的場景進(jìn)行使用。