join()函数是Python中用于将列表或元组的元素合并为字符串的常用函数,它的使用方法也非常简单,只需要在列表或元组的变量名前加上.join()即可。
使用示例
list1 = ["hello", "world"] str1 = '-'.join(list1) print(str1)
以上代码的运行结果为:hello-world。
join()函数还有一个参数sep,它可以用来指定分隔符,比如:
list1 = ["hello", "world"] str1 = '*'.join(list1) print(str1)
以上代码的运行结果为:hello*world。
join()函数还可以用于将元组中的元素合并成字符串,比如:
tuple1 = ("hello", "world") str1 = '-'.join(tuple1) print(str1)
以上代码的运行结果为:hello-world。
join()函数是Python中一个非常实用的函数,它可以帮助我们快速将列表或元组的元素合并为字符串,使用起来非常方便。