打开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)) #区间包头不包尾
  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))

提取:strip()、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表示分隔符出现的次数:['today', 'is', 'a good day']
  8. #2.splitlines(flag),按照换行符【\n,\r,\r\n】分割,结果为列表
  9. #flag可写可不写,False:忽略换行符,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,则替换的次数不超过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();如果字符串中至少有一个字符并且搜优的字符是数字或者字母的话,返回True
  8. print("".isalnum()) #False
  9. print("abc%".isalnum()) #False
  10. print("abc123".isalnum()) #True
  11. #3.isupper();如果字符串中至少有一个包含区分大小写的字符或者数字并且所有的字符都是大写,则返回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();如果字符串中至少有一个包含区分大小写的字符或者数字并且所有的字符都是小写,则返回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();判断字符串中是否只包含数字,如果是则返回True
  28. print("abc123".isdigit()) #False
  29. print("ABCabc123".isdigit()) #False
  30. print("123".isdigit()) #True
  31. #7.isnumeric();判断字符串中是否只包含数字字符,如果是则返回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();将字符串转化为字节的过程
  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();将字节类型转换为字符串的过程
  8. byte1 = str2.encode("gbk")
  9. print(type(byte1)) #<class 'bytes'>
  10. print(byte1.decode("gbk")) #hello 中文

ASCII码转换:ord()、chr()

  1. #ASCII码转换
  2. #ord();获取字符的整数表示
  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);创建字符映射的转换表
  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
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python中检查给定的字符串是否包含数字
java中判断字符串是否是一个整数(转载)
Python入门之一
Python中如何将字符串变成数字?
python 字符串所有操作
python中字符串内置函数的用法总结
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服