一、整形 int
1.1 -int #将字符串转化为数字类型
age = '20'
print(type(age))
num = int(age)
print(num)
print(type(num))
<class 'str'>
20
<class 'int'>
1.2 -bit_length() #当前数字的二进制是几位表示
num = age.bit_length()
print(num)
5
a = 1
print(a.bit_length())
1
b = 3
print(b.bit_length())
2
二、字符串 str
2.1
-capitalize() #将字符串首字母转化为大写
-title #转化为标题(所有首字母改为大写)
name = 'wangxiaolong'
print(name.capitalize())
Wangxiaolong
a = 'wang xiao long'
print(a.title())
Wang Xiao Long
2.2
-casefold() #将字符串中的大写转换为小写,更加厉害,可以转换全部
-lower() #将字符串中的大写转换为小写,只能转化英文
name = 'WANGxiaoLONG'
print(name.casefold())
wangxiaolong
2.3
-center() #设置宽度为为N并居中,空白填充
-ljust() #右边填充
-rjust()#左边填充
-zfill() # 左边填充,不能指定符号
name = 'wangxiaolong'
print(name.center(20, "_"))
print(name.center(20,"*"))
print(name.center(20,"o"))
____wangxiaolong____
****wangxiaolong****
oooowangxiaolongoooo
name = 'Riany'
print(name.ljust(20,'*'))
print(name.rjust(20,"*"))
print(name.zfill(20))
Riany***************
***************Riany
000000000000000Riany
2.4 -count() #计数在字符串出现的次数。后面的数字为字符串的下标
name = 'wangxiaolong'
print(name.count("a"))
print(name.count('a', 1, 6))
2
1
2.5
-endswith() #判断已什么结尾
-startswith() #判断已什么结尾
name = 'wangxiaolong'
print(name.endswith('g'))
print(name.endswith('x'))
True
False
print(name.startswith('w'))
print(name.startswith('x'))
True
False
2.6
-find() #从什么位置查找,不存在显示-1
-index() #和find语法类似,但是不存在直接报错
name = 'wangxiaolong'
print(name.find('a'))
print(name.find('a', 3, 12))
print(name.find('e'))
1
6
-1
print(name.index('a'))
print(name.index('a', 3, 12))
print(name.index('e'))
1
6
substring not found
2.7
-format() #占位符替换为值
-format_map #占位符替换为值
text = 'I an {name}, age {num}.'
print(text.format(name='Rainy', num = 20))
I an Rainy, age 20.
print(text.format_map({'name':'Diudiu', "num":18}))
I an Diudiu, age 18.
2.8
-isalnum() # 判断字符串是否有特殊符号
-isalpha() #判断字符串是否为字母
-isdecimal()#判断是否是数字,不能判断特殊符号
-isdigit()#判断是否是数字,可以判断特殊符号
-isnumeric()#判断是否为数字,可以判断中文数
-isidentifier() #判断是否已字母,下划线开头
-isprintable()#判断字符串打印有不可显示的字符
-isspace() #判断是否全部是空格
-ististitle() #判断是否为标题(首字母大写)
-islower() #判断是否是小写
-isupper(). #判断是否全部是大写
a = 'asda123_*'
print(a.isalnum())
False
b = 'asd123'
print(b.isalnum())
True
B = '123qwe'
c = 'qwe'
print(B.isalpha())
print(c.isalpha())
False
True
a = '123qwe'
b = '1234'
print(a.isdecimal())
print(a.isdigit())
print(b.isdecimal())
print(b.isdigit())
False
False
True
True
c = '③'
print(c.isdecimal())
print(c.isdigit())
print(c.isnumeric())
False
True
True
a = '二'
print(a.isnumeric())
print(a.isdigit())
print(a.isdecimal())
True
False
False
a = 'def'
print(a.isidentifier())
b = '_name'
print(b.isidentifier())
c = 'name_'
print(c.isidentifier())
d = 'qwe'
print(d.isidentifier())
f = '1a123'
print(f.isidentifier())
True a
True b
True c
True d
False f
a = 'asdasfdsfefd'
print(a.isprintable())
b = 'asdasfa\nasdwq'
print(b.isprintable())
c = 'asdffsd\t'
print(c.isprintable())
True
False
False
a = 'absf'
print(a.isspace())
b = 'a bc '
print(b.isspace())
c = ' abc'
print(c.isspace())
d = ' '
print(d.isspace())
False
False
False
True
a = 'Process finished with exit code'
print(a.istitle())
b = a.title()
print(b)
print(b.istitle())
False
Process Finished With Exit Code
True
name = "Rainy"
print(name.lower())
print(name.islower())
name = "Rainy"
name1 = 'QWE'
print(name.upper())
print(name.isupper())
print(name1.isupper())
2.9 expandtabs() #把字符串从0开始往右位移N个字符,当出现Tab(\t)就会自动补全到N位。
test = 'Username\tEmail\tPasswd\nwangxiaolong\t136@163.com\twangxiaolong\nliuyan\t182@163.com\tdiudiu'
print(test.expandtabs(15))
Username Email Passwd
wangxiaolong 136@163.com wangxiaolong
liuyan 182@163.com diudiu
2.10
-join #将字符串的每个元素按照指定分隔符进行拼接
a = '铥丢是最漂亮的'
print("~".join(a))
print('&'.join(a))
铥~丢~是~最~漂~亮~的
铥&丢&是&最&漂&亮&的
2.11
- upper() #小写转换为大写
name = "Rainy"
name1 = 'wangxiaolong'
print(name.upper())
print(name1.upper())
RAINY
WANGXIAOLONG
2.12 # 移除空白字符、/n、/t。指定字符
lstrip() #从左往右
rsprip()#从右往左
strip() #只要出现
name = ' Rainy '
print(name.lstrip())
print(name.rsplit())
print(name.strip())
Rainy
['Rainy']
Rainy
name1 = 'wangxiaolong'
print(name1.lstrip('wag'))
print(name1.rsplit('ong'))
print(name1.strip('wangxia'))
ngxiaolong
['wangxiaol', '']
olo
2.13
-maketrans # 创建对应关系
-translate #替换对应关系中的东西
a = 'wangxiaolong'
m = str.maketrans("wang",'1234')
print(a.translate(m))
1234xi2olo34
2.14 #按指定字符分割
partition() #从左往右分割第一个字符。不可以传参数(可以拿到指定字符)
rpartition()#从右往左分割第一个字符。不可传参数 (可以拿到指定字符)
split() #从左往右分割所有字符,可以指定分割次数(无法拿到指定字符)
rsplit #从右往左分割所有字符,可以指定分割次数(无法拿到指定字符)
splitlines() #只能通过换行符进行分割,True和False是否保留换行符。
a = 'testsadafsfsfs'
print(a.partition('s'))
print(a.rpartition('s'))
print(a.split('s',3))
print(a.rsplit('s',2))
('te', 's', 'tsadafsfsfs')
('testsadafsfsf', 's', '')
['te', 't', 'adaf', 'fsfs']
['testsadafsf', 'f', '']
a = 'sadfdsf\nfdsgfdgfdf\nasfdgfdgs'
print(a.splitlines())
print(a.splitlines(True))
print(a.splitlines(False))
['sadfdsf', 'fdsgfdgfdf', 'asfdgfdgs']
['sadfdsf\n', 'fdsgfdgfdf\n', 'asfdgfdgs']
['sadfdsf', 'fdsgfdgfdf', 'asfdgfdgs']
2.15 判断已什么开头或者结尾
startswith()#已什么开头
endswith()#已什么结尾
a = 'wangxiaolong'
print(a.startswith('wa'))
print(a.startswith('x'))
print(a.endswith('long'))
print(a.endswith('xia'))
True
False
True
False
2.16.
-swapcase() # 大小写转换
name = 'WangXiaoLong'
print(name.swapcase())
wANGxIAOlONG
2.17 替换
replace() #把某些参数替换为后面的,数字代表几次。
name = 'RainRainRain'
print(name.replace('R','W'))
print(name.replace('n','W',2))
列表的方法
列表转换为字符串
#1、列表中既有数字,也有字符串,需要手动for循环一个一个转换
a = [123,234,'asd','456']
s = ''
for i in a:
s = s + str(i)
print(s)
123234asd456
#2、列表中全部都是字符串的时候,用join
b = ['123','abc','dfg']
print("".join(b))
123abcdfg
插入元素
append(). #追加
insert() #向指定位置插入一个元素
li = [11,12,14,15]
li.append('Rainy')
li.append('qwe')
print(li)
[11, 12, 14, 15, 'Rainy', 'qwe']
# li.insert(0,9)
# print(li)
[9, 11, 12, 14, 15, 'Rainy', 'qwe']
删除列表中的值
clear() # 清空
pop()# #删除一个值,默认删除最后一个也可以指定下标位置,并可以获取到删除的值
remove() #删除一个指定的值,左边优先
del[] #删除一个值,按照下标删除(也支持切片操作)
li = [11,12,14,15]
li.clear() #清空
print(li)
[]
li = [11,12,14,15]
print(li.pop())
print(li)
15
[11, 12, 14]
print(li.pop(2))
print(li)
14
[11, 12]
li.remove(11)
print(li)
[12, 14, 15]
del li[1]
print(li)
列表反转
li = [11,16,12,14,15]
# li.reverse() #将列表反转
# print(li)
# li.sort() #从小到达排序
# print(li)
# li.sort(reverse=True) #从大到小
# print(li)
[15, 14, 12, 16, 11]
[11, 12, 14, 15, 16]
[16, 15, 14, 12, 11]
元组
书写格式
tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
一般写元组的时候,推荐在最后加入 ,
元素不可被修改,不能被增加或者删除索引
v = tu[0]
print(v)切片
v = tu[0:2]
print(v)可以被for循环,可迭代对象
tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
for item in tu:
print(item)
111
alex
(11, 22)
[(33, 44)]
True
33
44
- 转换
s = "asdfasdf0"
li = ["asdf","asdfasdf"]
tu = ("asdf","asdf")
v = tuple(s)
print(v)
v = tuple(li)
print(v)
v = list(tu)
print(v)
v = "_".join(tu)
print(v)
li = ["asdf","asdfasdf"]
li.extend((11,22,33,))
print(li)
6.元组的一级元素不可修改/删除/增加
tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
tu[3][0] = 0000
print(tu)
(111, 'alex', (11, 22), [0], True, 33, 44)
字典
# dict
dic = {
"k1": 'v1',
"k2": 'v2'
}
print(die)
{'k1': 'v1', 'k2': 'v2'}
1 根据序列,创建字典,并指定统一的值
print(dict.fromkeys([1,2,3,4,5,6,7],[123,312,321]))
1: 123, 2: 123, 3: 123, 4: 123, 5: 123, 6: 123, 7: 123}
2 -get() 根据Key获取值,key不存在时,可以指定默认值(None)
a = {1:'a',
3:'b',
4:'c',
5:'d'}
print(a.get(2,232143242342))
232143242342
输出keys,values
items() #全部输出keys,values
values() #只输出values
keys() # 只输出keys
a = {1:'a',
3:'b',
4:'c',
5:'d'}
for i,o in a.items():
print(i,o)
for i in a.values():
print(i)
for i in a.keys():
print(i)
3 b
4 c
5 d
a
b
c
d
1
3
4
5
删除
-pop(). #根据key的值删除value,当key不存在的时候,可以指定默认值。
-popitem() #随机删除一个值并可以获取。
a = {1:'a',
3:'b',
4:'c',
5:'d'}
v = a.pop(122,111111)
print(a, v)
{1: 'a', 3: 'b', 4: 'c', 5: 'd'} 111111
b = a.pop(5)
print(a)
print(b)
{1: 'a', 3: 'b', 4: 'c'}
d
v = a.popitem()
print(v)
print(a)
(5, 'd')
{1: 'a', 3: 'b', 4: 'c'}
设置值
setdefault(). #当设置的key已存在,不设置,并返回当前的值 #当设置的key不不存在,设置值并返回当前的值
a = {1:'a',
3:'b',
4:'c',
5:'d'}
v = a.setdefault(10,'e')
print(a)
print(v)
{1: 'a', 3: 'b', 4: 'c', 5: 'd', 10: 'e'}
e
v1 = a.setdefault(1,'t')
print(a)
print(v1)
{1: 'a', 3: 'b', 4: 'c', 5: 'd', 10: 'e'}
a
更新添加
update() #如果key存在,则更新。如果不存在则添加。
a = {1:'a',
3:'b',
4:'c',
5:'d'}
a.update({1:'w', 11:'qwe'})
print(a)
{1: 'w', 3: 'b', 4: 'c', 5: 'd', 11: 'qwe'}