Python3字符串基本操作与方法汇总(全)

深碍√TFBOYSˉ_ 2023-06-08 06:36 71阅读 0赞
  1. #字符串拼接 +
  2. str1 = "第一个str"
  3. str2 = "第二个str"
  4. print(str1+str2)
  5. # 输出:第一个str第二个str
  6. #输出多个相同字符串
  7. str3 = "good"
  8. str4 = str3*3
  9. print(str4)
  10. # 输出:goodgoodgood
  11. #通过下标访问字符串的某一字符
  12. str5 = "hello world!"
  13. print(str5[4])
  14. #字符串不可更改,不可变
  15. # str5[4] = 'a'
  16. # print(str5)
  17. # 输出:o
  18. #截取字符串中的一部分
  19. str6 = str5[6:11]
  20. print(str6)
  21. #截取从头到某一位置
  22. str6 = str5[:5]
  23. print(str6)
  24. #截取从某一位置到结尾
  25. str6 = str5[6:]
  26. print(str6)
  27. # 输出: world
  28. # hello
  29. # world!
  30. #判断字符串中是否有给定字符串(如果有返回True,没有返回False)
  31. str7 = "he"
  32. print("world" in str5)
  33. print(str7 in str5)
  34. print("ho" in str5)
  35. # 输出: true
  36. # true
  37. # false
  38. #字符串内换行
  39. print("hello\nworld!")
  40. #字符串中有很多换行,用\n不好阅读且繁琐(用''' ''')
  41. print('''
  42. hello
  43. world!
  44. ''')
  45. # 输出: hello
  46. # world!
  47. #
  48. # hello
  49. # world!
  50. #如果字符串中有很多字符都需要转义,需要添加很多 " \ " , python允许用r表示内部的字符串默认不转义
  51. print("\\t\\")
  52. print("\\\t\\")
  53. print(r"\\\t\\")
  54. # 输出: \t\
  55. # \ \
  56. # \\\t\\
  57. #eval(str)
  58. #将字符串str当成有效的表达式求值并返回计算结果
  59. print(eval("123"))
  60. print(eval("+123"))
  61. print(eval("-123"))
  62. #print(int("12-3")) #报错 因为"12-3"是字符串
  63. print(eval("12-3")) #能进行字符串内运算
  64. print(eval("12+3"))
  65. # 输出:
  66. # 123
  67. # 123
  68. # -123
  69. # 9
  70. # 15
  71. # len()
  72. #返回字符串的长度(字符个数)
  73. print(len(str5))
  74. # 输出:12
  75. # lower()
  76. #转换字符串中的大写字母到小写字母, 返回一个字符串是小写的,但是原字符串不变
  77. str7 = "HELLO woRLd!"
  78. print(str7.lower())
  79. # 输出:hello world!
  80. #upper
  81. #转换字符串中的小写字母到大写字母, 返回一个字符串是大写的,但是原字符串不变
  82. print(str7.upper())
  83. # 输出:HELLO WORLD!
  84. #swapcase
  85. #字符串中的大小写转换(大写变小写,小写变大写)
  86. print(str7.swapcase())
  87. # 输出:hello WOrlD!
  88. #capitalize
  89. #字符串中只有首字母大写,其余小写
  90. print(str7.capitalize())
  91. # 输出:Hello world!
  92. #title
  93. #字符串中每个单词的首字母大写
  94. print(str7.title())
  95. # 输出:Hello World!
  96. #center(width,fillchar)
  97. #居中填充,用字符填充到足够长度
  98. print(str5.center(40,"*"))
  99. # 输出:**************hello world!**************
  100. #ljuist(width,[,fillchar])
  101. #左对齐填充,默认空格填充
  102. print(str5.ljust(40,"*"))
  103. # 输出:hello world!****************************
  104. #rjuist(width,[,fillchar])
  105. #右对齐填充,默认空格填充
  106. print(str5.rjust(40,"*"))
  107. # 输出:****************************hello world!
  108. # zfill()
  109. #zero fill 用0填充 右对齐
  110. print(str5.zfill(40))
  111. # 输出:0000000000000000000000000000hello world!
  112. # count(str[,start][,end])
  113. #查询字符串中某字符串的个数
  114. print(str5.count("l"))
  115. #查询字符串中范围内某字符串的个数
  116. print(str5.count("l",5,10))
  117. # 输出: 3 1
  118. # find()
  119. # 检测字符串是否包含某字符串,可以指定范围,默认从头到尾,得到第一次出现的下标,没有返回-1
  120. print(str5.find("l"))
  121. print(str5.find("l",0,5))
  122. # 输出; 2 2
  123. # rfind()
  124. # 从右往左找
  125. print(str5.rfind("l"))
  126. # 输出: 9
  127. # index()
  128. # 和find方法一样,如果没有报错,不返回-1
  129. print(str5.index("l"))
  130. # rindex
  131. # 从右往左找
  132. print(str5.rindex("l"))
  133. # 输出:2 9
  134. # lstrip()
  135. # 截掉字符串左侧指定字符,默认为空格
  136. str8 = " hello world!"
  137. str9 = "****hello world"
  138. print(str8.lstrip())
  139. print(str9.lstrip("*"))
  140. # rstrip
  141. # 截掉字符串右侧指定字符,默认为空格
  142. # strip
  143. # 截掉字符串左右两侧指定字符,默认为空格
  144. # 输出:
  145. # hello world!
  146. # hello world

发表评论

表情:
评论列表 (有 0 条评论,71人围观)

还没有评论,来说两句吧...

相关阅读