list
列表
1. extend方法
在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
Append items from iterable to the end of the array.
aList = [123, 'xyz', 'zara', 'abc', 123]
bList = [2009, 'manni']
aList.extend(bList)
'''
Extended List : [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']
'''
2. append方法
在列表的最后添加元素
a = [1, 2, 3]
a.append(4)
'''
[1, 2, 3, 4]
'''
3. insert方法
在列表的指定位置添加元素
在第一个元素之前添加100:list.insert(0, 100)
在最后一个元素之前添加100:list.insert(-1, 100)
在最后一个元素之后添加100:length=len(list) list.insert(length, 100)
a = [1, 2, 3]
a.insert(0, 0)
'''
[0, 1, 2, 3]
'''
string
1. split方法
str.split(str="", num=string.count(str))
- str:分隔符,默认为所有空字符,包括空格、换行(\n)、制表符(\t)等
- num:分割次数。默认为-1,即分割所有
- 返回:分割后的字符串列表
str = "Line1-abcdef \nLine2-abc \nLine4-abcd"
print(str.split()) # 以空格为分隔符,包含 \n
print(str.split(' ', 1)) # 以空格为分隔符,分隔成两个
'''
['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
'''
2. join方法
Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串
a = ["1", "2", "3"]
print("-".join(a))
'''
1-2-3
'''
3. endswith方法
判断字符串是否以指定后缀结尾,如果是返回True,否则False
str = "1.png"
str.endswith("png")
'''
True
'''