python中的字符串连接函数join是如何进行处理的
下面的代码为什么执行出来为p-y-t-h-o-n呢?
为什么i和j位置互换,就只输出了一个‘-’呢?
i = '-'
j = 'python'
i = i.join(j)
print(i)
python中的字符串连接函数join是如何进行处理的
下面的代码为什么执行出来为p-y-t-h-o-n呢?
为什么i和j位置互换,就只输出了一个‘-’呢?
i = '-'
j = 'python'
i = i.join(j)
print(i)
string.join(字串、列表、元组等等都可以)
把可迭代参数的所有元素用string串连起来:
举个例子
join(iterable, /) method of builtins.str instance
Concatenate any number of strings.
The string whose method is called is inserted in between each given string.
The result is returned as a new string.
Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
>>> a = ['a','b','c']
>>> '=='.join(a)
'a==b==c'
>>> ''.join(a)
'abc'
>>> b = [1,2,3,4,5]
>>> '.'.join(map(str,b))
'1.2.3.4.5'
i = i.join(j) 当参数j长度为1时,只返回i;当然长度为0则返回空