在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"
这些方法可以根据需要和Python版本选择使用。