国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Python3之String字符串(填充、查找、提取、分割和合并、替換、判斷、前綴和后綴、編解碼、ASCII碼轉換)

重點掌握:len()、count()、center()、starswith()、find()、index()、strip()、replace()、split()、join()、isdigit()、ord()、chr()

填充:center()、ljust()、rjust()、zfill()

  1. #填充
  2. #1.center(width[,fillchar]);返回一個指定寬度的居中的字符串,width:指填充后整個字符串的長度,
  3. #fillchar:指需要被填充的字符串,默認為空格
  4. #注意:相當于生成新的字符串,填充的字符必須是精確的一個字符
  5. str1 = "hello"
  6. print(str1.center(20))
  7. print(str1.center(20,"#"))
  8. #2.ljust(width[,fillchar]);返回一個指定寬度的字符串,將原字符串居左對其,width是填充之后整個字符串的長度
  9. print(str1.ljust(20,"%"))
  10. #3.rjust(width[,fillchar]);返回一個指定寬度的字符串,將原字符串居左對其,width是填充之后整個字符串的長度
  11. print(str1.rjust(20,"%"))
  12. #4.zfill(width);返回一個指定寬度的字符串,將原字符串居右對其,width是填充之后整個字符串的長度,填充以零填充
  13. print(str1.rjust(20))

查找: find()、rfind()、index()、rindex()、max()、min()

  1. #查找
  2. #1.find(str[,start,end]);從左向右依次檢測,str是否在原字符串中,如果存在,則返回位置
  3. #特點:如果是查找到字符串,返回的是字符串中第一個字符所在的下標,如果找不到,則返回-1
  4. #注意:如果有重復子字符串,則返回第一個字符所在的位置
  5. str2 = "abcdefgh123hello"
  6. print(str2.find("hello"))
  7. print(str2.find("e"))
  8. print(str2.find("yyy"))
  9. print(str2.find("h",3,10)) #區(qū)間包頭不包尾
  10. #2.rfind();從右向左依次檢測,方式同上
  11. print(str2.rfind("hello"))
  12. print(str2.rfind("e"))
  13. #3.index();與find用法基本相同,但其如果查找不到,則直接報錯,而不是返回-1
  14. print(str2.index("hello"))
  15. print(str2.index("e"))
  16. #4.rindex()
  17. #5.max();返回原字符串中最大字母
  18. print(max(str2))
  19. #6.min()
  20. print(min(str2))

提?。簊trip()、lstrip()、rstrip()

  1. #提取
  2. #1.strip(str),使用str作為條件提取字符串,注意:只能去除兩端指定的字符【trim】
  3. str1 = "*******today is ****** a good day******"
  4. print(str1.strip("*")) #today is ****** a good day
  5. #2.lstrip(),去除左邊指定字符串
  6. print(str1.lstrip("*")) #today is ****** a good day******
  7. #3.rstrip(),去除右邊指定字符串
  8. print(str1.rstrip("*")) #*******today is ****** a good day

分割:split()

  1. #分割,使用指定字符串來分割原字符串,返回一個列表【由字符串轉化為列表的過程】
  2. #1.split()
  3. #注意:使用split進行分割時候,其中分割的字符串不能為空
  4. str1 = "today is a good day"
  5. print(str1.split(" ")) #['today', 'is', 'a', 'good', 'day']
  6. str2 = "hello"
  7. print(str1.split(" ",2)) #num表示分隔符出現(xiàn)的次數(shù):['today', 'is', 'a good day']
  8. #2.splitlines(flag),按照換行符【\n,\r,\r\n】分割,結果為列表
  9. #flag可寫可不寫,F(xiàn)alse:忽略換行符,True:保留換行符
  10. s1 = """today
  11. is
  12. a
  13. good
  14. day"""
  15. print(s1.splitlines(True)) #['today\n', 'is\n', 'a\n', 'good\n', 'day']

 合并:join()

  1. #合并
  2. #join(),將原字符串作為連接符號,將列表中的元素連接起來,作為一個字符串【列表轉換為字符串】
  3. str3 = "_"
  4. list1 = ["zhangdan","lisi","jack"]
  5. str4 = str3.join(list1)
  6. print(str4) #zhangdan_lisi_jack

 替換 :replace()

  1. #替換
  2. #replace(old,new[,max]);將原字符串中的old字符串替換為new字符串,如果指定了max,則替換的次數(shù)不超過max次
  3. #替換得到新的字符串
  4. str1 = "today is a good day"
  5. print(str1) #today is a good day
  6. print(str1.replace("good","bad")) #today is a bad day
  7. str2 = "today is a good good good day"
  8. print(str2) #today is a good good good day
  9. print(str2.replace("good","bad")) #today is a bad bad bad day
  10. print(str2.replace("good","bad",2)) #today is a bad bad good day

判斷:isalpha()、isalnum()、isupper()、islower()、istitle()、isdigit()、isnumeric()、isdecimal()、isspace()

  1. #判斷
  2. #全部返回的值是布爾值
  3. #1.isalpha();如果字符串至少有一個字符并且所有的字符都是字母的話,則返回True
  4. print("".isalpha()) #False
  5. print("abc".isalpha()) #True
  6. print("abc123".isalpha()) #False
  7. #2.isalnum();如果字符串中至少有一個字符并且搜優(yōu)的字符是數(shù)字或者字母的話,返回True
  8. print("".isalnum()) #False
  9. print("abc%".isalnum()) #False
  10. print("abc123".isalnum()) #True
  11. #3.isupper();如果字符串中至少有一個包含區(qū)分大小寫的字符或者數(shù)字并且所有的字符都是大寫,則返回True
  12. print("".isupper()) #False
  13. print("ABC".isupper()) #True
  14. print("ABCabc".isupper()) #False
  15. print("123".isupper()) #False
  16. print("ABC123".isupper()) #True
  17. #4.islower();如果字符串中至少有一個包含區(qū)分大小寫的字符或者數(shù)字并且所有的字符都是小寫,則返回True
  18. print("".islower()) #False
  19. print("abc".islower()) #True
  20. print("ABCabc".islower()) #False
  21. print("123".islower()) #False
  22. print("abc123".islower()) #True
  23. #5.istitle();如果對應字符串中的單詞是標題化的,則返回True
  24. print("this is a test".istitle()) #False
  25. print("This is a test".istitle()) #False
  26. print("This Is A Test".istitle()) #True
  27. #6.isdigit();判斷字符串中是否只包含數(shù)字,如果是則返回True
  28. print("abc123".isdigit()) #False
  29. print("ABCabc123".isdigit()) #False
  30. print("123".isdigit()) #True
  31. #7.isnumeric();判斷字符串中是否只包含數(shù)字字符,如果是則返回True。同上
  32. print("abc123".isnumeric()) #False
  33. print("ABCabc123".isnumeric()) #False
  34. print("123".isnumeric()) #True
  35. #8.isdecimal();判斷字符串中是否只包含十進制,如果是則返回True
  36. print("123".isdecimal()) #True
  37. print("123e4".isdecimal()) #False
  38. #9.isspace();判斷字符串中是否只包含空格,如果是則返回True
  39. print("abc 46".isspace()) #False
  40. print(" ".isspace()) #True

前綴和后綴:stratswith()、endswith()

  1. #前綴和后綴
  2. #startswith(str[,beg=0,end=len(string)])
  3. #判斷原字符串是否是以子字符串開頭的,如果beg和end指定值,則表示在指定的范圍內判斷
  4. str1 = "helloghfdh"
  5. print(str1.startswith("hello")) #True
  6. #endswith(str[,beg=0,end=len(string)])
  7. str1 = "helloghfdhello"
  8. print(str1.endswith("hello")) #True

編解碼:encode()、decode()

  1. #編解碼
  2. #encode();將字符串轉化為字節(jié)的過程
  3. str2 = "hello 中文"
  4. print(str2.encode()) #默認編碼格式為utf-8,國際編碼格式:b'hello \xe4\xb8\xad\xe6\x96\x87'
  5. print(str2.encode("utf-8")) #中國編碼格式:b'hello \xe4\xb8\xad\xe6\x96\x87'
  6. print(str2.encode("gbk")) #b'hello \xd6\xd0\xce\xc4'
  7. #decode();將字節(jié)類型轉換為字符串的過程
  8. byte1 = str2.encode("gbk")
  9. print(type(byte1)) #<class 'bytes'>
  10. print(byte1.decode("gbk")) #hello 中文

ASCII碼轉換:ord()、chr()

  1. #ASCII碼轉換
  2. #ord();獲取字符的整數(shù)表示
  3. print(ord("A"))
  4. print(ord("a"))
  5. #chr();將編碼轉換為對應的字符
  6. print(chr(65))
  7. print(chr(97))
  8. #將“hello”轉換為大寫
  9. s1 = "hello"
  10. s2 = ""
  11. for i in range(len(s1)):
  12. num = ord(s1[i])
  13. num -= 32
  14. ch = chr(num)
  15. s2 += ch #字符串拼接
  16. print(s2)
  17. #字符串的映射;相當于字典
  18. #maketrans(str1,str2);創(chuàng)建字符映射的轉換表
  19. #str1表示字符串,str2表示需要轉換的目標
  20. #translate(table)
  21. t = str.maketrans("ac","68")
  22. print(t) #{97: 54, 99: 56} , 0是48
  23. s3 = "hello abc"
  24. print(s3.translate(t)) #hello 6b8
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Python中檢查給定的字符串是否包含數(shù)字
java中判斷字符串是否是一個整數(shù)(轉載)
Python入門之一
Python中如何將字符串變成數(shù)字?
python 字符串所有操作
python中字符串內置函數(shù)的用法總結
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服