一穿香、整形 int
1.1 -int #將字符串轉(zhuǎn)化為數(shù)字類型
age = '20'
print(type(age))
num = int(age)
print(num)
print(type(num))
<class 'str'>
20
<class 'int'>
1.2 -bit_length() #當(dāng)前數(shù)字的二進(jìn)制是幾位表示
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() #將字符串首字母轉(zhuǎn)化為大寫(xiě)
-title #轉(zhuǎn)化為標(biāo)題(所有首字母改為大寫(xiě))
name = 'wangxiaolong'
print(name.capitalize())
Wangxiaolong
a = 'wang xiao long'
print(a.title())
Wang Xiao Long
2.2
-casefold() #將字符串中的大寫(xiě)轉(zhuǎn)換為小寫(xiě),更加厲害洗出,可以轉(zhuǎn)換全部
-lower() #將字符串中的大寫(xiě)轉(zhuǎn)換為小寫(xiě),只能轉(zhuǎn)化英文
name = 'WANGxiaoLONG'
print(name.casefold())
wangxiaolong
2.3
-center() #設(shè)置寬度為為N并居中痴颊,空白填充
-ljust() #右邊填充
-rjust()#左邊填充
-zfill() # 左邊填充樱调,不能指定符號(hào)
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() #計(jì)數(shù)在字符串出現(xiàn)的次數(shù)。后面的數(shù)字為字符串的下標(biāo)
name = 'wangxiaolong'
print(name.count("a"))
print(name.count('a', 1, 6))
2
1
2.5
-endswith() #判斷已什么結(jié)尾
-startswith() #判斷已什么結(jié)尾
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語(yǔ)法類似贝攒,但是不存在直接報(bào)錯(cuò)
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() # 判斷字符串是否有特殊符號(hào)
-isalpha() #判斷字符串是否為字母
-isdecimal()#判斷是否是數(shù)字,不能判斷特殊符號(hào)
-isdigit()#判斷是否是數(shù)字时甚,可以判斷特殊符號(hào)
-isnumeric()#判斷是否為數(shù)字隘弊,可以判斷中文數(shù)
-isidentifier() #判斷是否已字母哈踱,下劃線開(kāi)頭
-isprintable()#判斷字符串打印有不可顯示的字符
-isspace() #判斷是否全部是空格
-ististitle() #判斷是否為標(biāo)題(首字母大寫(xiě))
-islower() #判斷是否是小寫(xiě)
-isupper(). #判斷是否全部是大寫(xiě)
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開(kāi)始往右位移N個(gè)字符,當(dāng)出現(xiàn)Tab(\t)就會(huì)自動(dòng)補(bǔ)全到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 #將字符串的每個(gè)元素按照指定分隔符進(jìn)行拼接
a = '銩丟是最漂亮的'
print("~".join(a))
print('&'.join(a))
銩~丟~是~最~漂~亮~的
銩&丟&是&最&漂&亮&的
2.11
- upper() #小寫(xiě)轉(zhuǎn)換為大寫(xiě)
name = "Rainy"
name1 = 'wangxiaolong'
print(name.upper())
print(name1.upper())
RAINY
WANGXIAOLONG
2.12 # 移除空白字符开镣、/n、/t咽扇。指定字符
lstrip() #從左往右
rsprip()#從右往左
strip() #只要出現(xiàn)
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 # 創(chuàng)建對(duì)應(yīng)關(guān)系
-translate #替換對(duì)應(yīng)關(guān)系中的東西
a = 'wangxiaolong'
m = str.maketrans("wang",'1234')
print(a.translate(m))
1234xi2olo34
2.14 #按指定字符分割
partition() #從左往右分割第一個(gè)字符邪财。不可以傳參數(shù)(可以拿到指定字符)
rpartition()#從右往左分割第一個(gè)字符。不可傳參數(shù) (可以拿到指定字符)
split() #從左往右分割所有字符肌割,可以指定分割次數(shù)(無(wú)法拿到指定字符)
rsplit #從右往左分割所有字符卧蜓,可以指定分割次數(shù)(無(wú)法拿到指定字符)
splitlines() #只能通過(guò)換行符進(jìn)行分割,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 判斷已什么開(kāi)頭或者結(jié)尾
startswith()#已什么開(kāi)頭
endswith()#已什么結(jié)尾
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() # 大小寫(xiě)轉(zhuǎn)換
name = 'WangXiaoLong'
print(name.swapcase())
wANGxIAOlONG
2.17 替換
replace() #把某些參數(shù)替換為后面的弥奸,數(shù)字代表幾次。
name = 'RainRainRain'
print(name.replace('R','W'))
print(name.replace('n','W',2))
列表的方法
列表轉(zhuǎn)換為字符串
#1奋早、列表中既有數(shù)字盛霎,也有字符串,需要手動(dòng)for循環(huán)一個(gè)一個(gè)轉(zhuǎn)換
a = [123,234,'asd','456']
s = ''
for i in a:
s = s + str(i)
print(s)
123234asd456
#2耽装、列表中全部都是字符串的時(shí)候愤炸,用join
b = ['123','abc','dfg']
print("".join(b))
123abcdfg
插入元素
append(). #追加
insert() #向指定位置插入一個(gè)元素
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()# #刪除一個(gè)值,默認(rèn)刪除最后一個(gè)也可以指定下標(biāo)位置掉奄,并可以獲取到刪除的值
remove() #刪除一個(gè)指定的值规个,左邊優(yōu)先
del[] #刪除一個(gè)值,按照下標(biāo)刪除(也支持切片操作)
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)
列表反轉(zhuǎn)
li = [11,16,12,14,15]
# li.reverse() #將列表反轉(zhuǎn)
# print(li)
# li.sort() #從小到達(dá)排序
# print(li)
# li.sort(reverse=True) #從大到小
# print(li)
[15, 14, 12, 16, 11]
[11, 12, 14, 15, 16]
[16, 15, 14, 12, 11]
元組
書(shū)寫(xiě)格式
tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
一般寫(xiě)元組的時(shí)候姓建,推薦在最后加入 ,
元素不可被修改诞仓,不能被增加或者刪除索引
v = tu[0]
print(v)切片
v = tu[0:2]
print(v)可以被for循環(huán),可迭代對(duì)象
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
- 轉(zhuǎn)換
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.元組的一級(jí)元素不可修改/刪除/增加
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 根據(jù)序列速兔,創(chuàng)建字典墅拭,并指定統(tǒng)一的值
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() 根據(jù)Key獲取值,key不存在時(shí)涣狗,可以指定默認(rèn)值(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(). #根據(jù)key的值刪除value镀钓,當(dāng)key不存在的時(shí)候穗熬,可以指定默認(rèn)值。
-popitem() #隨機(jī)刪除一個(gè)值并可以獲取丁溅。
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'}
設(shè)置值
setdefault(). #當(dāng)設(shè)置的key已存在死陆,不設(shè)置,并返回當(dāng)前的值 #當(dāng)設(shè)置的key不不存在,設(shè)置值并返回當(dāng)前的值
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'}