Python运算符 - 连接

在Python中,连接运算符(+)用于将两个或多个字符串或列表组合在一起。

✏️ 语法

Python
operand1 + operand2

在上面的代码中,operand1operand2可以是字符串或列表。

📘 示例

Python
# 连接字符串
str1 = "Hello"
str2 = "World"
result = str1 + str2
print(result)

在上面的代码中,使用+运算符连接字符串"Hello"和"World",并打印结果"HelloWorld"。

Python
# 连接列表
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = list1 + list2
print(result)

在上面的代码中,使用+运算符连接列表[1, 2, 3][4, 5, 6],并打印结果[1, 2, 3, 4, 5, 6]