1、首字母大寫
語法:S.capitalize() ->str
title= "today is a good day"title_ca=title.capitalize()
print(title_ca)
結果:today is a good day
2、大寫轉小寫
1 語法:S.casefold() ->str2
3 title = "TODAY is a GOOD day"
4 title_ca =title.casefold()5 print(title_ca)
結果:Today is a good day
3、字符串居中
c = 'kong'ret= c.center(10,'*')
print(ret)
結果:***kong***
4、字符串子串數量統計
S.count(sub[, start[, end]]) -> inttitle= "today is a good day"title_ca= title.count('y',1,5)
print(title_ca)
結果:1
5、中文轉UTF-8
S.encode(encoding='utf-8', errors='strict') ->bytes
zw= '孔扎根'ut=zw.encode()
print(ut)
結果:b'\xe7\xa9\xba\xe6\x89\x8e\xe6\xa0\xb9'
6、字符串結束判斷
S.endswith(suffix[, start[, end]]) -> booltitle= "TODAY is a GOOD day"title_ca= title.endswith('day')
print(title_ca)
結果:True
7、TAB轉空格
S.expandtabs(tabsize=8) ->str
#默認是一個TAB轉8個空格
title= "TODAY\tis\ta\tGOOD\tday"title_ca=title.expandtabs()
print(title_ca)
結果:TODAYis a GOOD day
8、查找字符串的位置
S.find(sub[, start[, end]]) -> inttitle= "TODAY\tis\ta\tGOOD\tday"title_ca= title.find('s')
print(title_ca)
結果:7
9、字符串格式化
S.format(*args, **kwargs) ->str
#可傳入元組或字典
title= "{}\tis\ta\t{day_type}\tday"title_ca= title.format('TODAY',day_type='GOOD')
print(title_ca)
結果:TODAYis a GOOD day
10、字符串格式化,從字典輸入(format_map)
S.format_map(mapping) ->str
#輸入參數為字典,循環讀字典中的列表
maping_name={'name':['alex','join'],'age':[18,19]
}for x in range(2):
print('my name is {},and i is {} old'.format(maping_name['name'][x],maping_name['age'][x]))
結果:
my nameis alex,and i is 18old
my nameis join,and i is 19 old
11、字符串的索引位置
S.index(sub[, start[, end]]) -> int#查找Y的索引位置,從0開始數
title= "TODAY\tis\ta\tGOOD\tday"title_ca= title.index('Y')
print(title_ca)
結果:4
12、字符串中至少有一個數字
S.isalnum() -> bool#字符串不能有空格,否則失敗
title= "22TODAY"title_ca=title.isalnum()
print(title_ca)
結果:True
13、字符串中至少有一個字母
S.isalpha() -> bool#字符串不能有空格或TAB鍵
title= "22TODAY"title_ca=title.isalnum()
print(title_ca)
結果:True
14、字符串是否為數值
num = "1"#unioncode
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())
結果:
True
True
True
num= "1"#全角
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())
結果:
True
True
True
num= b"1"#byteprint(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())
結果:
True
AttributeError:'bytes' object has no attribute 'isdecimal'AttributeError:'bytes' object has no attribute 'isnumeric'num= "IV"#羅馬字符
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())
結果:
False
False
False
num= "四"#漢字
print(num.isdigit())
print(num.isdecimal())
print(num.isnumeric())
結果:
False
False
True
支持的字符:
isdigit:支持 unioncode,全角,byte,漢字
isdecimal:支持 unioncode,全角,
isnumeric:支持 unioncode,全角,漢字
報錯:
isdigit不會報錯,后兩種在byte判斷時會報錯
15、判斷字符串是否為有效標識符(可做為函數名稱)
#S.isidentifier() -> boolt_code= 'test'print(t_code.isidentifier())
結果:返回True
t_code= '23test'print(t_code.isidentifier())
結果:返回False
16、判斷字符串是否全小寫
#S.islower() -> boolt_code= 'kongzhagen'print(t_code.islower())
結果:返回True
t_code= 'kongzHagen'print(t_code.islower())
結果:返回False
17、判斷字符串是否全整型數字
#S.isnumeric() -> boolt_code= '123'print(t_code.isnumeric())
結果:返回True
t_code= '123d'print(t_code.isnumeric())
結果:返回False
t_code= '123.123'print(t_code.isnumeric())
結果:返回False
18、判斷所有字符是否可打印
#S.isprintable() -> boolt_code= '123KKK'print(t_code.isprintable())
返回:True
t_code= ''print(t_code.isprintable())
返回:True
t_code= 'KKK\n\t'print(t_code.isprintable())
返回:False
19、判斷字符中是否全為空格
#S.isspace() -> boolt_code= ' 'print(t_code.isspace())
結果:返回True
t_code= '123 KKK'print(t_code.isspace())
結果:返回False
20、判斷字符串是否為標題格式(首字母大寫)
#S.istitle() -> bool
t_code = 'Today Is A Good Day'
print(t_code.istitle())
結果:True
t_code = 'Today Is A Good day'
print(t_code.istitle())
結果:False
t_code = 'TODAY IS'
print(t_code.istitle())
結果:False
21、判斷字符串是否全大寫
#S.isupper() -> boolt_code= 'Today Is A Good day'print(t_code.isupper())
結果:False
t_code= 'TODAY IS'print(t_code.isupper())
結果:True
22、字符串連接
#S.join(iterable) ->str
t_code= 'Today Is A Good Day't1_code= '.'.join(t_code)
print(t1_code)
結果:T.o.d.a.y. .I.s. .A. .G.o.o.d. .D.a.y
23、左對齊,達不到指定長度,右則填充
#S.ljust(width[, fillchar]) ->str
t_code= 'Today Is A Good Day'print(t_code.ljust(22,'*'))
結果:Today Is A Good Day***
24、轉小寫
#S.lower() ->str
t_code= 'Today Is A Good Day'print(t_code.lower())
結果:todayis a good day
25、左邊去除指定字符,默認為空格
# S.lstrip([chars]) ->str
t_code= 'Today Is A Good Day'print(t_code.lstrip('T'))
結果:oday Is A Good Day
t_code= 'Today Is A Good Day'print(t_code.lstrip())
結果:Today Is A Good Day
26、
maketrans
27、partition
按指定的字符拆分字符串,分頭、分隔串、尾,未找到指定的分隔符,頭返回自己,后面兩個返回空
#S.partition(sep) ->(head, sep, tail)
t_code= 'TodayIs A Good Day'print(t_code.partition('a'))
結果:('Tod', 'a', 'yIs A Good Day')
print(t_code.partition('P'))
結果:('TodayIs A Good Day', '', '')
28、replace:字符串替換
將老字符串替換為新字符串,可指定替換次數
#S.replace(old, new[, count]) ->str
t_code= 'TodayIs A Good Day,To today'print(t_code.replace('T','M',2))
結果:ModayIs A Good Day,Mo today
29、rfind:返回查詢到的字符串的最大索引
#S.rfind(sub[, start[, end]]) -> int
t_code = 'TodayIs A Good Day,To today'print(t_code.rfind('d'))
結果:24
30、rindex:類似rfind,但如果沒找到會報錯
#S.rindex(sub[, start[, end]]) -> int
t_code = 'TodayIs A Good Day,To today'
print(t_code.rindex('p'))
結果:
Traceback (most recent call last):
File "C:/51py/day1/study.py", line 90, in
print(t_code.rindex('p'))
ValueError: substring not found
31、rjust:右對齊,左側填充字符
#S.rjust(width[, fillchar]) -> str
t_code = 'Today'print(t_code.rjust(10,'*'))
結果:*****Today
32、rpartition:類似partition,如果未找到字符串,則空值在左邊
#S.rpartition(sep) ->(head, sep, tail)
t_code= 'Today is a good day'print(t_code.rpartition('isa'))
結果:('', '', 'Today is a good day')
33、rsplit:分割字符串,從右邊開始
#S.rsplit(sep=None, maxsplit=-1) ->list of strings
t_code= 'Today is a good day'print(t_code.rsplit('o',1))
結果:['Today is a go', 'd day']
34、rstrip:右邊去空格
#S.rstrip([chars]) ->str
t_code= 'Today is a good day'print(t_code.rstrip())
結果: Todayis a good day
35、splitlines:方法返回一個字符串的所有行列表,可選包括換行符的列表(如果num提供,則為true)
#S.splitlines([keepends]) ->list of strings
t_code= 'Today\n is\n a\n good\n day'print(t_code.splitlines())
print(t_code.splitlines(0))
print(t_code.splitlines(1))
結果:
['Today', 'is', 'a', 'good', 'day']
['Today', 'is', 'a', 'good', 'day']
['Today\n', 'is\n', 'a\n', 'good\n', 'day']
36、startswith:如果字符串以指定的字符為前綴,則返回true,否則返回false
#S.startswith(prefix[, start[, end]]) -> boolt_code= 'Today\n is\n a\n good\n day'print(t_code.startswith('Today'))
結果:True
37、strip:去除字符串前后的空格
#S.strip([chars]) ->str
t_code= 'Today\n is\n a\n good\n day'print(t_code.strip())
結果:
Todayisa
good
day
38、swapcase:大小寫互轉
#S.swapcase() -> str
t_code = ' Today Is a Good Day '
print(t_code.swapcase())
結果: tODAY iS A gOOD dAY
39、title:返回的字符串為title格式,首字母大寫
#S.title() ->str
t_code= 'today is a Good Day'print(t_code.title())
結果:Today Is A Good Day
40、maketrans:用于創建字符映射的轉換表,兩個參數為長度相等的字符串
#B.maketrans(frm, to) ->translation table
intab= "aeiou"outab= "1234k"trantab=t_code.maketrans(intab,outab)
print(trantab)
結果:{97: 49, 111: 52, 117: 107, 101: 50, 105: 51}
41、translate:根據參數table給出的表轉換字符中的字符
# S.translate(table) ->str
t_code= 'today is a Good Day'trantab= {97:49}
print(t_code.translate(trantab))
結果: tod1yis 1 Good D1y
42、ord與chr是配對函數
>>> chr(65)'A'
>>> ord('A')65
43、upper:將字符串轉為大寫
#S.upper() ->str
t_code= 'today is a Good Day'print(t_code.upper())
結果:TODAY IS A GOOD DAY
44、zfill:數字填零
#S.zfill(width) ->str
t_code= '123'print(t_code.zfill(5))
結果:00123
45、匯總
str = 'https://www.baidu. com234'# print(str.capitalize()) # 第一個字母大寫
# print(str.count('w')) # 字符在字符串中出現的次數
# print(str.endswith('com')) # 字符串是否以com結尾
# print(str.expandtabs(tabsize=2)) # 字符串中的tab轉為兩個空格
# print(str.find('bai')) # 返回字符串中bai的索引
# print(str.rfind('bai')) # 返回字符串中bai的索引,從右邊找
# print(str.index('bai')) # 返回字符串中bai的索引,未找到會報錯
# print(str.rindex()) # 返回字符串中bai的索引,未找到會報錯(從右邊找)
# print(str.isalnum()) # 如果所有字符都是數字或字母,則返回True
# print(str.isalpha()) # 如果所有字符都是字母,則返回True
# print(str.isnumeric()) # 如果所有字符都是數字,則返回T
# print(str.isdecimal()) # 可解釋為十進制數,則返回True
# print(str.isdigit()) # 可解釋為數字,則返回True
# print(str.islower()) # 字符串中的字母都小寫,則返回True(可以有其它字符)
# print(str.isupper()) # 字符串中的字母都大寫,則返回True(可以有其它字符)
# print(str.isspace()) # 字符串中全是空格,則返回True
# print(str.istitle()) # 如果字符串是標題化的,則返回True(每個單詞首字母大寫,其它小寫)
# print(str.ljust(100)) # 字符串左對齊,長度100
# print(str.rjust(100)) # 字符串右對齊,長度100
# print(str.lower()) # 所有字符轉小寫
# print(str.lstrip()) # 去掉字符串左邊的空格
print str.replace('t','2',2) # 將p 替換為2,替換2次
print str.rfind('o') # 從右邊查找第一個o所在的位置
print str.rindex('o') # 從右邊查找第一個o所在的位置的索引
print str.partition('du') # 從左邊找到第一個du,并以之分隔字符串,返回列表
print str.rstrip() # 去掉右邊的空格
print str.split('w',2) # 以w為分隔符,切分字符串
print str.splitlines() # 以行做為分隔符
print str.startswith('http') # 是否以http開頭
print str.swapcase() # 翻轉大小寫
print str.title() # 將string標題化,所有單詞首字母大寫,其它小寫
print str.upper() # 所有字母大寫
print str.zfill(150) # 返回150個字符,原字符串右對齊,前面填充0