为什么在Python中使用string.join(list)而不是list.join(string)?
join()是一个字符串方法,使用它时,分隔符字符串会在任意序列上迭代,从而形成每个元素的字符串表示形式,并在元素之间插入自己。
简而言之,这是因为join和string.join()都是可在任何可迭代对象上使用的通用操作,并且是字符串方法。
由于它是一种字符串方法,因此join()既可以用于Unicode字符串,也可以用于普通ASCII字符串。
示例用法string.join(list)
-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> test_string = "test"
>>> test_string.join("1234")
'1test2test3test4'
>>>在上面的示例中,字符串“test”与作为连接参数提供的每个字符连接在一起。
-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> test_string = "test"
>>> test_string.join("---")
'-test-test-'
>>>热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短