温馨提示×

python中concat函数的用法是什么

小亿
98
2023-12-12 18:09:49
栏目: 编程语言

在Python中,concat函数用于连接(合并)字符串、列表、元组或其他可迭代对象。它将两个或多个可迭代对象连接起来,形成一个新的可迭代对象。

在Python中,可以通过"+"操作符或使用concat函数来实现连接操作。

以下是concat函数的用法示例:

  1. 连接字符串:
string1 = "Hello"
string2 = "World"
result = concat([string1, string2])
print(result)
# 输出:HelloWorld
  1. 连接列表:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = concat([list1, list2])
print(result)
# 输出:[1, 2, 3, 4, 5, 6]
  1. 连接元组:
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
result = concat([tuple1, tuple2])
print(result)
# 输出:(1, 2, 3, 4, 5, 6)

需要注意的是,concat函数返回的是一个新的可迭代对象,并不会修改原始的可迭代对象。

0