~玉米糊~
慢慢来,也会很快。 非宁静无以志学,学什么都一样,慢慢打基础,找规律、认真、坚持,其余的交给时间。

1. 如何将列表中的元素(字符串类型的值)连接在一起(首尾相接)

a = ['a', 'b', 'c', 'd', 'e']
s = ''
print(s.join(a))
s = '+'
print(s.join(a))

  

abcde
a+b+c+d+e

2. 字符串的join方法的作用是什么,使用join应该注意些什么,请举例说明

# 组装pth
dirs = '', 'usr', 'local', 'nginx', ''
print(dirs)
linuxPath = '/'.join(dirs)
print(linuxPath)
windowPath = 'C:' + '\\'.join(dirs)
print(windowPath)

num = [1,2,3]
print(s.join(num))
# TypeError: sequence item 0: expected str instance, int found

  

('', 'usr', 'local', 'nginx', '')
/usr/local/nginx/
C:\usr\local\nginx\
Traceback (most recent call last):
File "E:/py3/study/tests/test.py", line 16, in <module>
print(s.join(num))
TypeError: sequence item 0: expected str instance, int found

注意:连接的每个部分都必须为str,如果不是需要先转换。

posted on 2022-04-19 21:18  yuminhu  阅读(32)  评论(0编辑  收藏  举报