原网址:https://www.geeksforgeeks.org/join-function-python
Python中的join()函数
join()是一个字符串方法,它返回被子字符串连接的字符串。
语法:
string_name.join(iterable) string_name:这是被连接的子字符串。
参数:The join() method takes join()方法需要可迭代的元素来一次返回它的一个成员,比如列表,元组,字符串,字典和集合
返回值: join()方法返回一个被子字符串连接的字符串。
Type Error: 如果这个可迭代元素包含任何不是字符串的值,join()函数就会抛出TypeError。
下面的程序解释了join()方法是如何工作的:
# Python program to demonstrate the # use of join function to join list # elements with a character. list1 = ['1','2','3','4'] s = "-" # joins elements of list1 by '-' # and stores in sting s s = s.join(list1) # join use to join a list of # strings to a separator s print(s)
输出:
1-2-3-4
用空字符连接
# Python program to demonstrate the # use of join function to join list # elements without any separator. # Joining with empty separator list1 = ['g','e','e','k', 's'] print("".join(list1))
输出:
geeks
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/205579.html原文链接:https://javaforall.net
