内置函数(少量)

1, endwith()

如果字符串是以什么結尾  则输出true

String = “sdasdasd”

String.endswith(d)

>>true

2, find()

在字符串中搜索指定值并返回他原来的位置

find(value,start,end)

value   要搜索的值

start    开始搜索的位置  默认为0

end     结束搜索的位置  默认为字符串的结尾

a = sting.find(d)

print(a)

>>1

3, index()

用法和find相同,区别在于  当用index查找不到要找的元素时,会报错

但find不会

4, join()

获取可迭代的对象中的所有项,并把他们连接成一个字符串

Join前为分隔符

A =(“ad”,“sd”,“nm”)

X = “#”.join(A)

Print(X)

>>ad#sd#nm

5, strip()

删除字符串两头的空白

I = “ sdsds ”

Print(i.strip())

>>sdsds

6, split()

通过指定分隔符对字符串进行切片,如果参数有指定值,则按参数分割

String.split(‘z’,num)

用字符串中元素z来分割字符串  并组成列表  num 代表分割一次

Str = ‘sasda /sds /sdasd’

Print(str.split( ))

Print(str.split(‘ ‘,1)

>>[‘sasda’,’、/ds’,/’sdasd’]

>>[‘sasda’,’/sas/saasd’]

7, replace()

用某元素代替指定元素

8, append()

在指定字符串末尾加上指定元素

9, clear()

清空字典

10,            insert()

将指定对象插入到列表的指定位置

Lis1=[‘ad’,’ds’,12’,’sd’]

List1.insert(1,sd)

Print(lis1)

>>[’ad’,’sd’,’ds’,’12’]

11,            sort()

将数字列表从小到大排列

M = [1,25,2,32,52]

Print(m.sort())

>>[1,2,25,32,52]

12,            remove()

移除列表中的某个元素

H = {‘123’,’sxs’,’zx’}

Print(h.remove(sxs))

>>{‘123’,’zx’}

13,            reverse()

将列表元素反向

Print(h.reserve())

>>{‘zx’,’123’}

14,            copy()

拷贝对象(复制)

A={1.2.3}

B=copy,copy(a)

Print(a)

Print(B)

>>{1,2,3}

>>{1,2,3}

15,            fromkeys()

创建一个新字典,

A =(‘s’,’d’,’a’)

Dict2 = dict.fromkeys(a,12)#以a中元素为键,并使每个键对应值为12

Print(dict2)

>>{‘s’:12,’d’:12,’a’:12}

16,            get()

返回字典中的所有键

17,            pop

移出列表中的一个元素(默认为最后一个)

18,            popitem

返回并删除字典中的最后一对键和值

19,            setdefault()

用法和get一样 区别在于如果建不存在于字典中 setdefault将会添加新的键并设值为默认值  但get不会

20,            values()

返回字典中所有值

原文地址:https://www.cnblogs.com/zl-mengxiang/p/13832038.html