一、 int类型:
1. bit_length
原型:def bit_length(self):
功能:返回int型所占字节数
示例:
a = 124
print(bin(a))print(a.bit_length())
结果:
0b11111007
二、str类型
1. capitalize
原型:def capitalize(self):
功能:字符串首字母大写
示例:
s = "abc"
print(s.capitalize())
结果:"Abc"
2.center
原型:def center(self, width, fillchar=None):
功能:字符串居中两边填充fillchar
示例:
s = "abc"
print(s.center(10))print(s.center(10, "_"))
结果:
abc___abc____
3.count
原型:def count(self, sub, start=None, end=None):
功能:统计在字符串中出现的次数
示例:
s = "aabbccaddeeaa"
print(s.count("a"))print(s.count("a", 3))print(s.count("a", 0, 5))
结果:
5
3
2
4.encode
原型:def encode(self, encoding='utf-8', errors='strict'):
功能:将字符串转换成其他编码的字符串,默认编码格式为‘utf-8’
示例:
s = "老男孩"
print(s.encode())print(s.encode("gbk"))
结果:
b'\xe8\x80\x81\xe7\x94\xb7\xe5\xad\xa9'b'\xc0\xcf\xc4\xd0\xba\xa2'
5.endswith
原型:def endswith(self, suffix, start=None, end=None):
功能:判断字符串时候以制定的suffix结尾
示例:
s = "aabbccaddeeaa"
print(s.endswith("a"))print(s.endswith("b"))print(s.endswith("aa"))print(s.endswith("aa", 0, 5))
结果:
True
False
True
False
6.expandtabs
原型:def expandtabs(self, tabsize=8):
功能:将字符串中的tab换成8个空格,tabsize为替换空格数
示例:
s = "aaa\tbbb"
print(s.expandtabs())print(s.expandtabs(3))
结果:
aaa bbb
aaa bbb
7.find
原型:def find(self, sub, start=None, end=None):
功能:找到sub在该字符串中的位置,若未找到,返回-1
示例:
s = "aaabcdeffaw"
print(s.find("a"))print(s.find("a", 5))print(s.find("a", 5, 8))
结果:
09
-1
8.format
原型:def format(*args, **kwargs):
功能:字符串格式化输出
示例:
s = "{0} is {1} years old"
print(s.format("king", "12"))
结果:
king is 12 years old
9.index
原型:def index(self, sub, start=None, end=None):
功能:用法同find,若找不到,则报错
示例:
s = "aaabcdeffaw"
print(s.index("a"))print(s.index("a", 5))print(s.index("a", 5, 8))
结果:
09Traceback (most recent call last):
File"F:/oldboy/Day3/test1.py", line 26, in
print(s.index("a", 5, 8))
ValueError: substringnot found
10.isalnum
原型:def isalnum(self):
功能:检测字符串是否由字母和数字组成
示例:
s = "123"
print(s.isalnum())
s1= "123.00"
print(s1.isalnum())
结果:
True
False
11.isalpha
原型:def isalpha(self):
功能:判断字符串是否仅为字母组成
示例:
s = "asdadadasdJLJLJLJ"
print(s.isalpha())
s1= "fadfa1jofa2213joaUQOWU"
print(s1.isalpha())
s2= "uaodufa79UOUO!@"
print(s2.isalpha())
s3= "dfafaf 342380!@#$ YIUO"
print(s3.isalpha())
s4= "你好"
print(s4.isalpha())
结果:
True
False
False
False
True#好奇围观此结果,原因待查
12.isdecimal
原型:def isdecimal(self):
功能:字符串是否只包含十进制字符
示例:
s = "123"
print(s.isdecimal())
s1= "123.00"
print(s1.isdecimal())
s2= "123aa"
print(s2.isdecimal())
结果:
True
False
False
13.isdigit
原型:def isdigit(self):
功能:检测字符串是否只由数字组成。
示例:
s = "123a"
print(s.isdigit())
s1= "123"
print(s1.isdigit())
结果:
False
True
14.islower
原型:def islower(self):
功能:检测字符串是否由小写字母组成。
示例:
s = "fadfa"
print(s.islower())
s1= "fadfa111"
print(s1.islower())
s2= "dafQWE"
print(s2.islower())
结果:
True
True
False
15.isnumeric
原型:def isnumeric(self):
功能:如果字符串中的所有字符都是数字此方法返回true,否则返回false。
示例:
s = "132123"
print(s.isnumeric())
s1= "123123.00"
print(s1.isnumeric())
s2= "1fadfa"
print(s2.isnumeric())
结果:
True
False
False
16.isprintable
原型:def isprintable(self):
功能:判断字符串中所有字符是否都属于可见字符
示例:
s = "\ndfa\t"
print(s.isprintable())
s1= "fasdf fadfa"
print(s1.isprintable())
结果:
False
True
17.isspace
原型:def isspace(self):
功能:判断字符串是否全为空格
示例:
s = "\t\t\n"
print(s.isspace())
s1= " "
print(s1.isspace())
s2= "dfad adfa"
print(s2.isspace())
结果:
True
True
False
18.istitle
原型:def istitle(self):
功能:检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。
示例:
s = "This is an apple"
print(s.istitle())
s1= "This Is An Apple"
print(s1.istitle())
结果:
False
True
19.isupper
原型:def isupper(self):
功能:检测字符串中所有的字母是否都为大写
示例:
s = "Apple"
print(s.isupper())
s1= "APPLE"
print(s1.isupper())
结果:
False
True
20.join
原型:def join(self, iterable):
功能:将序列中的元素以指定的字符连接生成一个新的字符串
示例:
s = "afdafadf"
print(",".join(s))
s1= ["this", "is", "an", "apple"]print(" ".join(s1))
结果:
a,f,d,a,f,a,d,f
thisis an apple
21.ljust
原型:def ljust(self, width, fillchar=None):
功能:返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串
示例:
s = "fadfafa"
print(s.ljust(10, "_"))print(s.ljust(5, "_"))
结果:
fadfafa___
fadfafa
22.lower
原型:def lower(self):
功能:返回字符串的小写字母版
示例:
s = "This is an apple"
print(s.lower())
结果:
this is an apple
23.lstrip
原型:def lstrip(self, chars=None):
功能:去除字符串左侧空格
示例:
s = "This is an apple"
print(s.lstrip())
结果:
This is an apple (注意:右侧空格到这里)
24.partition
原型:def partition(self, sep):
功能:根据指定的分隔符将字符串进行分割
示例:
s = "http://www.baidu.com"
print(s.partition("//"))
结果:
('http:', '//', 'www.baidu.com')
25.replace
原型:def replace(self, old, new, count=None):
功能:把字符串中的 old(旧字符串) 替换成 new(新字符串)
示例:
s = "aadfdfsewaa dfaodf aa"
print(s.replace("aa", "**"))print(s.replace("aa", "**", 2))
结果:
**dfdfsew** dfaodf **
**dfdfsew** dfaodf aa
26.split
原型:def split(self, sep=None, maxsplit=-1):
功能:通过指定分隔符对字符串进行切片
示例:
s = "1|2|3|4|5|6|7"
print(s.split("|"))print(s.split("|", 3))
结果:
['1', '2', '3', '4', '5', '6', '7']
['1', '2', '3', '4|5|6|7']
27.startswith
原型:def startswith(self, prefix, start=None, end=None):
功能:判断字符串时候以制定的suffix开始
示例:
s = "dfaeadfea"
print(s.startswith("d"))print(s.startswith("d", 1))
结果:
True
False
28.strip
原型:def strip(self, chars=None):
功能:去除字符串左右两边的指定字符
示例:
s = "werwe fdfadfa"
print(s.strip())
s1= "aaa dfadf aaa"
print(s1.strip("a"))
结果:
werwe fdfadfa
dfadf
29.title
原型:def title(self):
功能:将字符串所有单词首字母变为大写
示例:
s = "this is an apple"
print(s.title())
结果:
This Is An Apple
30.upper
原型:def upper(self):
功能:将字符串小写字母变大写
示例:
s = "this is an apple"
print(s.upper())
结果:
THIS IS AN APPLE
三、tuple类型
1.count
原型:def count(self, value):
功能:输出value在tuple中出现的次数
示例:
t = ("11", "22", "33", "11")print(t.count("11"))print(t.count("22"))
结果:
2
1
2.index
原型:def index(self, value, start=None, stop=None):
功能:返回value在tuple中第一次出现的索引,若不存在,报错
示例:
t = ("11", "22", "33", "11")print(t.index("11"))print(t.index("11", 1))print(t.index("11", 1, 3))
结果:
03Traceback (most recent call last):
File"F:/oldboy/Day3/test1.py", line 98, in
print(t.index("11", 1, 3))
ValueError: tuple.index(x): xnot in tuple
四、list类型
1.append
原型:def append(self, p_object):
功能:向列表尾部追加一个元素
示例:
l = ["11", "22", "33", "44"]l.append("123")print(l)
结果:
['11', '22', '33', '44', '567', '123']
2.clear
原型:def clear(self):
功能:清空列表元素
示例:
l = ["11", "22", "33", "44"]
l.clear()print(l)
结果:
[]
3.copy
原型:def copy(self):
功能:创建一个列表的副本
示例:
l = ["11", "22", "33", "44"]
li=l.copy()print(l)print(li)
li.append("123")print(l)print(li)
结果:
['11', '22', '33', '44']
['11', '22', '33', '44']
['11', '22', '33', '44']
['11', '22', '33', '44', '123']
4.count
原型:def count(self, value):
功能:元素value在列表中出现的次数
示例:
l = ["11", "22", "33", "44", "11"]print(l.count("11"))print(l.count("22"))print(l.count("55"))
结果:
2
10
5.extend
原型:def extend(self, iterable):
功能:向列表追加元素,iterable可迭代的类型
示例:
l = ["11", "22", "33", "44", "11"]
l.extend("123")print(l)
l1= ["aa", "bb", "cc", "dd"]
l.extend(l1)print(l)
结果:
['11', '22', '33', '44', '11', '1', '2', '3']
['11', '22', '33', '44', '11', '1', '2', '3', 'aa', 'bb', 'cc', 'dd']
6.index
原型:def index(self, value, start=None, stop=None):
功能:返回value在列表中的索引,若不存在,报错
示例:
l = ["11", "22", "33", "44", "11"]print(l.index("11"))print(l.index("11", 1))print(l.index("11", 1, 3))
结果:
04Traceback (most recent call last):
File"F:/oldboy/Day3/test1.py", line 95, in
print(l.index("11", 1, 3))
ValueError:'11' is not in list
7.insert
原型:def insert(self, index, p_object):
功能:在列表指定位置插入元素
示例:
l = ["11", "22", "33", "44", "11"]#l.insert("123")
l.insert(1, "456")print(l)
l.insert(3, "789")print(l)
结果:
['11', '456', '22', '33', '44', '11']
['11', '456', '22', '789', '33', '44', '11']
8.pop
原型:def pop(self, index=None):
功能:移除列表指定索引的元素,并返回该元素的值
示例:
l = ["11", "22", "33", "44", "11"]
a=l.pop()print(a)print(l)
b= l.pop(1)print(b)print(l)
结果:
11['11', '22', '33', '44']22['11', '33', '44']
9.remove
原型:def remove(self, value):
功能:移除列表中第一次出现的值为value的元素
示例:
l = ["11", "22", "33", "44", "11"]
l.remove("11")print(l)
l.remove("33")print(l)
结果:
['22', '33', '44', '11']
['22', '44', '11']
10.reverse
原型:def reverse(self):
功能:反序输出列表
示例:
l = ["11", "22", "33", "44", "11"]
l.reverse()print(l)
结果:
['11', '44', '33', '22', '11']
11.sort
原型:def sort(self, key=None, reverse=False):
功能:对列表进行排序
示例:
l = ["11", "22", "33", "44", "11"]
l.sort()print(l)
l1= ["11", "22", "33", "44", "11"]
l1.sort(reverse=True)print(l1)
结果:
['11', '11', '22', '33', '44']
['44', '33', '22', '11', '11']
五、dict
1.clear
原型:def clear(self):
功能:清空字典
示例:
d = {"k1": 123, "k2": 456}print(d)
d.clear()print(d)
结果:
{'k2': 456, 'k1': 123}
{}
2.copy
原型:def copy(self):
功能:为字典创建一个副本,副本为一个新的字典
示例:
d = {"k1": 123, "k2": 456}print(d)
d1=d.copy()print(d1)print(id(d))print(id(d1))
结果:
{'k2': 456, 'k1': 123}
{'k2': 456, 'k1': 123}46462792
46463240
3.fromkeys
原型:def fromkeys(*args, **kwargs):
功能:字典类的静态方法,生成一个新的字典
示例:
d = dict.fromkeys(["k1", "k2", "k3"], "123")print(d)
d["k1"] = "456"
print(d)
结果:
{'k3': '123', 'k1': '123', 'k2': '123'}
{'k3': '123', 'k1': '456', 'k2': '123'}
4.get
原型:def get(self, k, d=None):
功能:获取key为k的值,若不存在,则为d值
示例:
d = {"k1": 123, "k2": 456}
v= d.get("k1")print(v)
v1= d.get("k3")print(v1)
v2= d.get("k3", "789")print(v2)
结果:
123None789
5.items
原型:def items(self):
功能:返回字典k->v键值对
示例:
d = {"k1": 123, "k2": 456}
kv=d.items()print(kv)for k, v ind.items():print(k, v)
结果:
dict_items([('k1', 123), ('k2', 456)])
k1123k2456
6.keys
原型:def keys(self):
功能:返回字典的键
示例:
d = {"k1": 123, "k2": 456}
k=d.keys()print(k)for k ind.keys():print(k)
结果:
dict_keys(['k1', 'k2'])
k1
k2
7.pop
原型:def pop(self, k, d=None):
功能:移除键为k的元素,并返回k所对应的值
示例:
d = {"k1": 123, "k2": 456}
a= d.pop("k1")print(a)print(d)
结果:
123{'k2': 456}
8.popitem
原型:def popitem(self):
功能:移除字典的最后一个元素(键值对),并返回该键值对
示例:
d = {"k1": 123, "k2": 456}
a=d.popitem()print(a)print(d)
结果:
('k2', 456)
{'k1': 123}
9.setdefault
原型:def setdefault(self, k, d=None):
功能:向字典追加元素,键为k,值为d
示例:
d ={}
d.setdefault("k1")print(d)
d.setdefault("k2", "111")print(d)
结果:
{'k1': None}
{'k1': None, 'k2': '111'}
10.update
原型:def update(self, E=None, **F):
功能:向字典追加元素
示例:
d ={}
d.update({"k2": "111"})print(d)
d.update({"k1": "111"})print(d)
d.update(enumerate("123"))print(d)
结果:
{'k2': '111'}
{'k1': '111', 'k2': '111'}
{'k1': '111', 0: '1', 'k2': '111', 2: '3', 1: '2'}
11.values
原型:def values(self):
功能:返回字典中元素的值
示例:
d = {"k1": 123, "k2": 456}
v=d.values()print(v)for v ind.values():print(v)
结果:
dict_values([123, 456])123
456
六、set
1. add
原型:def add(self, *args, **kwargs):
功能:向set中添加元素,如果元素已存在,不进行任何操作
示例:
se =set()
se.add("11")print(se)
se.add("22")print(se)
se.add("11")print(se)
结果:
{'11'}
{'11', '22'}
{'11', '22'}
2. clear
原型:clear(self, *args, **kwargs):
功能:清空set中的元素
示例:
se =set()
se.add("11")print(se)
se.add("22")print(se)
se.add("11")print(se)
se.clear()print(se)
结果:
{'11'}
{'22', '11'}
{'22', '11'}
set()
3. copy
原型:copy(self, *args, **kwargs):
功能:拷贝生成一个新的set
示例:
se =set()
se.add("11")print(se)
se.add("22")print(se)
ce=se.copy()print(ce)
结果:
{'11'}
{'22', '11'}
{'22', '11'}
4. difference
原型:difference(self, *args, **kwargs):
功能:找出a中存在,b中不存在的元素,返回一个新的set
示例:
se = {"11", "22", "33", "44"}
be= {"22", "33", "44", "55"}
de=se.difference(be)print(se)print(be)print(de)
结果:
{'44', '22', '11', '33'}
{'44', '55', '22', '33'}
{'11'}
5. difference_update
原型:difference_update(self, *args, **kwargs):
功能:找出a中存在,b中不存在的元素,更新a
示例:
se = {"11", "22", "33", "44"}
be= {"22", "33", "44", "55"}
de=se.difference_update(be)print(se)print(be)print(de)
结果:
{'11'}
{'55', '33', '44', '22'}
None
6. discard
原型:discard(self, *args, **kwargs):
功能:移除指定元素,若元素不存在,不报错
示例:
se = {"11", "22"}print(se)
se.discard("33")print(se)
se.discard("11")print(se)
结果:
{'22', '11'}
{'22', '11'}
{'22'}
7. intersection
原型:intersection(self, *args, **kwargs):
功能:取交集
示例:
se = {"11", "22", "33", "44"}
be= {"22", "33", "44", "55"}
de=se.intersection(be)
e= se &beprint(se)print(be)print(de)print(e)
结果:
{'44', '33', '11', '22'}
{'44', '55', '33', '22'}
{'44', '33', '22'}
{'44', '33', '22'}
8. intersection_update
原型:intersection_update(self, *args, **kwargs):
功能:取交集,并更新到原set
示例:
se = {"11", "22", "33", "44"}
be= {"22", "33", "44", "55"}
de=se.intersection_update(be)print(se)print(be)print(de)
se= se &beprint(se)
结果:
{'33', '22', '44'}
{'55', '33', '22', '44'}
None
{'33', '22', '44'}
9. isdisjoint
原型:isdisjoint(self, *args, **kwargs):
功能:判断是否有交集,没有返回True,有返回False
示例:
se = {"11", "22", "33", "44"}
be= {"22", "33", "44", "55"}print(se.isdisjoint(be))
ce= {"55", "66", "77", "88"}print(se.isdisjoint(ce))
结果:
False
True
10. issubset
原型:issubset(self, *args, **kwargs):
功能:是否是子序列
示例:
se = {"11", "22", "33", "44"}
be= {"22", "33", "44", "55"}
ce= {"11", "22", "33", "44", "55"}print(se.issubset(be))print(se.issubset(ce))
结果:
False
True
11. issuperset
原型:issuperset(self, *args, **kwargs):
功能:是否为父序列
示例:
se = {"11", "22", "33", "44"}
be= {"22", "33", "44", "55"}
ce= {"11", "22", "33", "44", "55"}print(be.issuperset(se))print(ce.issuperset(se))
结果:
False
True
12. pop
原型:pop(self, *args, **kwargs):
功能:移除元素,若set为空,则报错
示例:
se = {"11", "22"}
se.pop()print(se)
结果:
{'22'}
13. remove
原型:remove(self, *args, **kwargs):
功能:删除元素,若不存在,则报错
示例:
se = {"11", "22"}
se.remove("11")print(se)
se.remove("11")print(se)
结果:
{'22'}
File"F:/oldboy/Day4/test.py", line 7, in se.remove("11")
KeyError:'11'
14. symmetric_difference
原型:symmetric_difference(self, *args, **kwargs):
功能:对称差集
示例:
se = {"11", "22", "33", "44"}
be= {"22", "33", "44", "55"}
de=se.symmetric_difference(be)print(de)
结果:
{'55', '11'}
15. symmetric_difference_update
原型:symmetric_difference_update(self, *args, **kwargs):
功能:对称差集,并更新到原set
示例:
se = {"11", "22", "33", "44"}
be= {"22", "33", "44", "55"}
de=se.symmetric_difference_update(be)print(se)print(be)print(de)
结果:
{'11', '55'}
{'22', '44', '55', '33'}
None
16. union
原型:union(self, *args, **kwargs):
功能:并集
示例:
se = {"11", "22", "33", "44"}
be= {"22", "33", "44", "55"}
de=se.union(be)print(se)print(be)print(de)
de= se |beprint(de)
结果:
{'22', '33', '44', '11'}
{'22', '33', '44', '55'}
{'22', '33', '11', '44', '55'}
{'22', '33', '11', '44', '55'}
17. update
原型:def update(self, *args, **kwargs):
功能:更新set
示例:
se = {"11", "22"}
se.update("33")print(se)
se.update(["11", "33", "44"])print(se)
结果:
{'3', '22', '11'}
{'44', '3', '33', '22', '11'}
七、类型间转换
1.str->list
s = "1234567890"
print(type(s))
l=list(s)print(type(l))print(l)
打印结果
['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
2. tuple->list
t = (12, 34, "56", "78")print(type(t))
l=list(t)print(type(l))print(l)
打印结果
[12, 34, '56', '78']
3.str->tuple
s = "234567890"
print(type(s))
t=tuple(s)print(type(t))print(t)
打印结果
('2', '3', '4', '5', '6', '7', '8', '9', '0')
4. str->dict、tuple->dict、list->dict
s = "234567890"
print(type(s))
ds=dict(enumerate(s))print(type(ds))print(ds)print("*".center(15, "*"))
t=tuple(s)print(type(t))
dt=dict(enumerate(t))print(type(dt))print(dt)print("*".center(15, "*"))
l=list(s)print(type(l))
dl=dict(enumerate(l))print(type(dl))print(dl)
打印结果
{0:'2', 1: '3', 2: '4', 3: '5', 4: '6', 5: '7', 6: '8', 7: '9', 8: '0'}***************
{0:'2', 1: '3', 2: '4', 3: '5', 4: '6', 5: '7', 6: '8', 7: '9', 8: '0'}***************
{0:'2', 1: '3', 2: '4', 3: '5', 4: '6', 5: '7', 6: '8', 7: '9', 8: '0'}