重點掌握:len()、count()、center()、starswith()、find()、index()、strip()、replace()、split()、join()、isdigit()、ord()、chr()
填充:center()、ljust()、rjust()、zfill()
#填充 #1.center(width[,fillchar]);返回一個指定寬度的居中的字符串,width:指填充后整個字符串的長度, #fillchar:指需要被填充的字符串,默認為空格 #注意:相當于生成新的字符串,填充的字符必須是精確的一個字符 str1 = "hello" print(str1.center(20)) print(str1.center(20,"#")) #2.ljust(width[,fillchar]);返回一個指定寬度的字符串,將原字符串居左對其,width是填充之后整個字符串的長度 print(str1.ljust(20,"%")) #3.rjust(width[,fillchar]);返回一個指定寬度的字符串,將原字符串居左對其,width是填充之后整個字符串的長度 print(str1.rjust(20,"%")) #4.zfill(width);返回一個指定寬度的字符串,將原字符串居右對其,width是填充之后整個字符串的長度,填充以零填充 print(str1.rjust(20))
查找: find()、rfind()、index()、rindex()、max()、min()
#查找 #1.find(str[,start,end]);從左向右依次檢測,str是否在原字符串中,如果存在,則返回位置 #特點:如果是查找到字符串,返回的是字符串中第一個字符所在的下標,如果找不到,則返回-1 #注意:如果有重復子字符串,則返回第一個字符所在的位置 str2 = "abcdefgh123hello" print(str2.find("hello")) print(str2.find("e")) print(str2.find("yyy")) print(str2.find("h",3,10)) #區(qū)間包頭不包尾 #2.rfind();從右向左依次檢測,方式同上 print(str2.rfind("hello")) print(str2.rfind("e")) #3.index();與find用法基本相同,但其如果查找不到,則直接報錯,而不是返回-1 print(str2.index("hello")) print(str2.index("e")) #4.rindex() #5.max();返回原字符串中最大字母 print(max(str2)) #6.min() print(min(str2))
提?。簊trip()、lstrip()、rstrip()
#提取 #1.strip(str),使用str作為條件提取字符串,注意:只能去除兩端指定的字符【trim】 str1 = "*******today is ****** a good day******" print(str1.strip("*")) #today is ****** a good day #2.lstrip(),去除左邊指定字符串 print(str1.lstrip("*")) #today is ****** a good day****** #3.rstrip(),去除右邊指定字符串 print(str1.rstrip("*")) #*******today is ****** a good day
分割:split()
#分割,使用指定字符串來分割原字符串,返回一個列表【由字符串轉化為列表的過程】 #1.split() #注意:使用split進行分割時候,其中分割的字符串不能為空 str1 = "today is a good day" print(str1.split(" ")) #['today', 'is', 'a', 'good', 'day'] str2 = "hello" print(str1.split(" ",2)) #num表示分隔符出現(xiàn)的次數(shù):['today', 'is', 'a good day'] #2.splitlines(flag),按照換行符【\n,\r,\r\n】分割,結果為列表 #flag可寫可不寫,F(xiàn)alse:忽略換行符,True:保留換行符 s1 = """today is a good day""" print(s1.splitlines(True)) #['today\n', 'is\n', 'a\n', 'good\n', 'day']
合并:join()
#合并 #join(),將原字符串作為連接符號,將列表中的元素連接起來,作為一個字符串【列表轉換為字符串】 str3 = "_" list1 = ["zhangdan","lisi","jack"] str4 = str3.join(list1) print(str4) #zhangdan_lisi_jack
替換 :replace()
#替換 #replace(old,new[,max]);將原字符串中的old字符串替換為new字符串,如果指定了max,則替換的次數(shù)不超過max次 #替換得到新的字符串 str1 = "today is a good day" print(str1) #today is a good day print(str1.replace("good","bad")) #today is a bad day str2 = "today is a good good good day" print(str2) #today is a good good good day print(str2.replace("good","bad")) #today is a bad bad bad day print(str2.replace("good","bad",2)) #today is a bad bad good day
判斷:isalpha()、isalnum()、isupper()、islower()、istitle()、isdigit()、isnumeric()、isdecimal()、isspace()
#判斷 #全部返回的值是布爾值 #1.isalpha();如果字符串至少有一個字符并且所有的字符都是字母的話,則返回True print("".isalpha()) #False print("abc".isalpha()) #True print("abc123".isalpha()) #False #2.isalnum();如果字符串中至少有一個字符并且搜優(yōu)的字符是數(shù)字或者字母的話,返回True print("".isalnum()) #False print("abc%".isalnum()) #False print("abc123".isalnum()) #True #3.isupper();如果字符串中至少有一個包含區(qū)分大小寫的字符或者數(shù)字并且所有的字符都是大寫,則返回True print("".isupper()) #False print("ABC".isupper()) #True print("ABCabc".isupper()) #False print("123".isupper()) #False print("ABC123".isupper()) #True #4.islower();如果字符串中至少有一個包含區(qū)分大小寫的字符或者數(shù)字并且所有的字符都是小寫,則返回True print("".islower()) #False print("abc".islower()) #True print("ABCabc".islower()) #False print("123".islower()) #False print("abc123".islower()) #True #5.istitle();如果對應字符串中的單詞是標題化的,則返回True print("this is a test".istitle()) #False print("This is a test".istitle()) #False print("This Is A Test".istitle()) #True #6.isdigit();判斷字符串中是否只包含數(shù)字,如果是則返回True print("abc123".isdigit()) #False print("ABCabc123".isdigit()) #False print("123".isdigit()) #True #7.isnumeric();判斷字符串中是否只包含數(shù)字字符,如果是則返回True。同上 print("abc123".isnumeric()) #False print("ABCabc123".isnumeric()) #False print("123".isnumeric()) #True #8.isdecimal();判斷字符串中是否只包含十進制,如果是則返回True print("123".isdecimal()) #True print("123e4".isdecimal()) #False #9.isspace();判斷字符串中是否只包含空格,如果是則返回True print("abc 46".isspace()) #False print(" ".isspace()) #True
前綴和后綴:stratswith()、endswith()
#前綴和后綴 #startswith(str[,beg=0,end=len(string)]) #判斷原字符串是否是以子字符串開頭的,如果beg和end指定值,則表示在指定的范圍內判斷 str1 = "helloghfdh" print(str1.startswith("hello")) #True #endswith(str[,beg=0,end=len(string)]) str1 = "helloghfdhello" print(str1.endswith("hello")) #True
編解碼:encode()、decode()
#編解碼 #encode();將字符串轉化為字節(jié)的過程 str2 = "hello 中文" print(str2.encode()) #默認編碼格式為utf-8,國際編碼格式:b'hello \xe4\xb8\xad\xe6\x96\x87' print(str2.encode("utf-8")) #中國編碼格式:b'hello \xe4\xb8\xad\xe6\x96\x87' print(str2.encode("gbk")) #b'hello \xd6\xd0\xce\xc4' #decode();將字節(jié)類型轉換為字符串的過程 byte1 = str2.encode("gbk") print(type(byte1)) #<class 'bytes'> print(byte1.decode("gbk")) #hello 中文
ASCII碼轉換:ord()、chr()
#ASCII碼轉換 #ord();獲取字符的整數(shù)表示 print(ord("A")) print(ord("a")) #chr();將編碼轉換為對應的字符 print(chr(65)) print(chr(97)) #將“hello”轉換為大寫 s1 = "hello" s2 = "" for i in range(len(s1)): num = ord(s1[i]) num -= 32 ch = chr(num) s2 += ch #字符串拼接 print(s2) #字符串的映射;相當于字典 #maketrans(str1,str2);創(chuàng)建字符映射的轉換表 #str1表示字符串,str2表示需要轉換的目標 #translate(table) t = str.maketrans("ac","68") print(t) #{97: 54, 99: 56} , 0是48 s3 = "hello abc" print(s3.translate(t)) #hello 6b8