在Python中,有多種方法可以用來拼接字符串。以下是一些常用的方法:
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # 輸出 "Hello World"
name = "Alice"
age = 30
result = "My name is {} and I am {} years old.".format(name, age)
print(result) # 輸出 "My name is Alice and I am 30 years old."
name = "Bob"
age = 25
result = f"My name is {name} and I am {age} years old."
print(result) # 輸出 "My name is Bob and I am 25 years old."
join()
方法:words = ["Hello", "World"]
result = " ".join(words)
print(result) # 輸出 "Hello World"
這些方法可以根據(jù)需要和Python版本選擇使用。