老莫的笔记本  
  
查看: 102|回复: 0

python 字符串基础

[复制链接]

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
发表于 2023-9-7 23:36:23 | 显示全部楼层 |阅读模式



  1. # # This is a sample Python script.
  2. #
  3. # # Press Shift+F10 to execute it or replace it with your code.
  4. # # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
  5. #
  6. #
  7. # def print_hi(name):
  8. #     # Use a breakpoint in the code line below to debug your script.
  9. #     print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.
  10. #
  11. #
  12. # # Press the green button in the gutter to run the script.
  13. # if __name__ == '__main__':
  14. #     print_hi('PyCharm_main')
  15. #
  16. # # See PyCharm help at https://www.jetbrains.com/help/pycharm/
  17. #

  18. # 参考文献
  19. # https://blog.csdn.net/m0_51769031/article/details/127322960
  20. # 字符串与 切片
  21. # 一个概念: python 具有一个变量池,所有变量值相同的变量 所对应的值其实 是同一个值;
  22. # 当产生这个变量时,如果 变量池中已经有这个值,那么 默认这个ID(值的地址)是一样的
  23. # 1.切片的用法
  24. p = 'ABCDEFGHIGKLMN'

  25. n = '0123456789'

  26. # 至前 内置函数
  27. # len(x)   #返回字符串x的长度
  28. # str(x)   #将任意类型的x转化为字符串类型
  29. # chr(x)   #返回Unicode编码为x的字符
  30. # ord(x)   #返回字符x的Unicode编码
  31. # hex(x)   #将整数x转化为十六进制数
  32. # oct(x)   #将整数x转化为八进制数

  33. # 1.find 成功则返回下标(最左侧首次出现) 不成功返回 -1
  34. # 参数说明 str.find(sub[,start[,end]])
  35. # str表示被查找的字符串。sub表示查找的子串。start表示开始索引,缺省时为0。end表示结束索引,缺省时为字符串的长度。
  36. print(n.find('3'))  # 3
  37. print(n.find('b'))  # -1
  38. print(p.find('CD'))  # 2

  39. # 2.rfind  同find  但是查的的是最后一次出现的位置
  40. print(p.rfind('C'))

  41. # 3.index 存在则返回下标(最左侧首次出现) 不存在则抛出异常
  42. print(p.index('C'))
  43. # print(p.index('c'))

  44. # 4. rindex(最右侧 最后出现) 同index


  45. # 5.count()  #用来返回一个字符串在另一个字符串中出现的次数,若不存在则返回
  46. m = 'pcyuasdoiuuuuusdasoaso'
  47. print('u出现了%d次' % (m.count('u')))

  48. # 字符串和列表的相互转化
  49. # split 将字符串格式化为列表
  50. # str.split([sep[, maxsplit]]) 参数说明:
  51. # sep是分割符,maxsplit是最大分割次数。如果传入的参数sep为空或为None,则默认使用空格作为分隔符
  52. # 注意这里的最大分割数,并不是代表分割为几段 二十代表分割出来几段 如果是1 那就是2个段

  53. s = 'bird,fish,monkey,rabbi,666'
  54. print(s.split(','))  # ['bird', 'fish', 'monkey', 'rabbi', '666']
  55. print(s.rsplit(','))  # ['bird', 'fish', 'monkey', 'rabbi', '666']
  56. print(s.split(',', 1))  # ['bird', 'fish,monkey,rabbi,666']
  57. print(s.rsplit(',', 2))  # ['bird,fish,monkey', 'rabbi', '666']

  58. # partition()    #以指定字符串为分隔符将原字符串分割为3个部分,分隔符之前的字符串,分隔符字符串和分隔符之后的字符串
  59. # rpartition()   #以指定字符串为分隔符将原字符串分割为3个部分,分隔符之前的字符串,分隔符字符串和分隔符之后的字符串
  60. # 就是生成一个长度为3的列表  左侧是 指定字符之前的, 右侧是指定字符之后的  中间是指定的字符 各位列表中的一项
  61. print(p.partition('B'))  # ('A', 'B', 'CDEFGHIGKLMN')
  62. print(p.rpartition('B'))  # ('A', 'B', 'CDEFGHIGKLMN')

  63. # join 列表转为字符串 插入指定字符
  64. s_list = ['name', 'yesa', 'vvv']
  65. print('--'.join(s_list))  # name--yesa--vvv

  66. # lower()     #将字符串转换为小写字符串
  67. # uppper()    #将字符串转换为大写字符串
  68. # capitalize()    #将字符串首字母变为大写
  69. # title()         #将字符串中每个单词的首字母都变为大写
  70. # swapcase()      #将字符串中的字符大小写互换

  71. # replace()      #替换字符串中指定字符或子字符串  全局替换
  72. sm = 'rorororoftt'
  73. cm = sm.replace('t', '溜溜溜')
  74. print(cm)
  75. # rorororof溜溜溜溜溜溜


  76. # strip()         #删除字符串两端空白字符 或者指定字符串
  77. # rstrip()        #删除字符串右端空白字符 或者指定字符串
  78. # lstrip()        #删除字符串左端空白字符 或者指定字符串
  79. bm = "=====das==da==="
  80. bb = bm.strip('==')
  81. print(bb)
  82. # das==da



  83. # startswith()   #判断字符串是否以指定字符开始
  84. # endswith()      #判断字符串是否以指定字符结束
  85. a = bm.startswith('==')
  86. print(a)
  87. # True



  88. # isupper()       #是否全为大写
  89. # islower()       #是否全为小写
  90. # isdigit()       #是否全为数字
  91. # isalnum()       #是否全为字母或汉字或数字
  92. # isalpha()       #是否全为字母或汉字


  93. # center()         #字符串居中对齐
  94. # ljust()          #字符串居左对齐
  95. # rjust()          #字符串居右对齐
  96. # zfill()          #输出指定宽度,不足的左边填0



  97. # format 格式方法
  98. # 1 format()的默认顺序和指定顺序

  99. cstr = '这是{}年级,{}班的学生{}'.format('三','8','周杰伦')
  100. print(cstr)
  101. # 这是三年级,8班的学生周杰伦

  102. cstr = '这是{0}年级,{2}班的学生{1}'.format('三','周杰伦','八')
  103. print(cstr)
  104. # 这是三年级,八班的学生周杰伦 注意后方元组中下标从0 开始

  105. # 当使用format()方法格式化字符串的时候,首先需要在"{}”中输入“:”,然后在":"之后分别设置<填充字符><对齐方式><宽度>。

  106. cstr = "{:y^12}".format( '内容')
  107. print(cstr)
  108. # yyyyy内容yyyyy

  109. cstr2 = "{:f<12}".format('内容')
  110. print(cstr2)
  111. # 内容ffffffffff


  112. cstr2 = "{:f>12}".format('内容')
  113. print(cstr2)
  114. # ffffffffff内容

  115. # 利用format()方法设置数字的保留位数。
  116. cstr = "{:0>6.4}".format('23.1234567')
  117. print(cstr)
  118. # 0023.1


  119. cstr ="{:.4}".format('23.1234567')
  120. print(cstr)
  121. # 23.1

  122. cstr2 ="{:.2f}".format('23.1234567')
  123. print(cstr2)
  124. #

  125. cstr3 ="{:.5d}".format('23.1234567')
  126. print(cstr3)




复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表