python字符串

字符串是一个有序的字符的集合,用于存储和表示基本的文本信息,’ ‘或’’ ‘’或’’’ ‘’’中间包含的内容称之为字符串

创建:

  1. s = 'Hello,Eva!How are you?'

特性:

 

    1. 按照从左到右的顺序定义字符集合,下标从0开始顺序访问,有序

 

 

    1. 可以进行切片操作

 

    1. 不可变,字符串是不可变的,不能像列表一样修改其中某个元素,所有对字符串的修改操作其实都是相当于生成了一份新数据。

 

补充:

1.字符串的单引号和双引号都无法取消特殊字符的含义,如果想让引号内所有字符均取消特殊意义,在引号前面加r,如name=r’l\thf’

 

字符串的常用操作

字符串操作方法有非常多,但有些不常用 ,我们只讲重要的一些给大家,其它100年都用不上的有兴趣可以自己研究

    1. def capitalize(self):
    2. 首字母大写
    3. def casefold(self):
    4. 把字符串全变小写
    5. >> > c = 'Alex Li'
    6. >> > c.casefold()
    7. 'alex li'
    8. def center(self, width, fillchar=None):
    9. >> > c.center(50, "-")
    10. '---------------------Alex Li----------------------'
    11. def count(self, sub, start=None, end=None):
    12. """
    13. S.count(sub[, start[, end]]) -> int
    14. >>> s = "welcome to apeland"
    15. >>> s.count('e')
    16. 3
    17. >>> s.count('e',3)
    18. 2
    19. >>> s.count('e',3,-1)
    20. 2
    21. def encode(self, encoding='utf-8', errors='strict'):
    22. """
    23. 编码,日后讲
    24. def endswith(self, suffix, start=None, end=None):
    25. >> > s = "welcome to apeland"
    26. >> > s.endswith("land") 判断以什么结尾
    27. True
    28. def find(self, sub, start=None, end=None):
    29. """
    30. S.find(sub[, start[, end]]) -> int
    31. Return the lowest index in S where substring sub is found,
    32. such that sub is contained within S[start:end]. Optional
    33. arguments start and end are interpreted as in slice notation.
    34. Return -1 on failure.
    35. """
    36. return 0
    37. def format(self, *args, **kwargs): # known special case of str.format
    38. >> > s = "Welcome {0} to Apeland,you are No.{1} user."
    39. >> > s.format("Eva", 9999)
    40. 'Welcome Eva to Apeland,you are No.9999 user.'
    41. >> > s1 = "Welcome {name} to Apeland,you are No.{user_num} user."
    42. >> > s1.format(name="Alex", user_num=999)
    43. 'Welcome Alex to Apeland,you are No.999 user.'
    44. def format_map(self, mapping):
    45. """
    46. S.format_map(mapping) -> str
    47. Return a formatted version of S, using substitutions from mapping.
    48. The substitutions are identified by braces ('{' and '}').
    49. """
    50. 讲完dict再讲这个
    51. def index(self, sub, start=None, end=None):
    52. """
    53. S.index(sub[, start[, end]]) -> int
    54. Return the lowest index in S where substring sub is found,
    55. such that sub is contained within S[start:end]. Optional
    56. arguments start and end are interpreted as in slice notation.
    57. Raises ValueError when the substring is not found.
    58. """
    59. def isdigit(self):
    60. """
    61. S.isdigit() -> bool
    62. Return True if all characters in S are digits
    63. and there is at least one character in S, False otherwise.
    64. """
    65. return False
    66. def islower(self):
    67. """
    68. S.islower() -> bool
    69. Return True if all cased characters in S are lowercase and there is
    70. at least one cased character in S, False otherwise.
    71. """
    72. def isspace(self):
    73. """
    74. S.isspace() -> bool
    75. Return True if all characters in S are whitespace
    76. and there is at least one character in S, False otherwise.
    77. """
    78. def isupper(self):
    79. """
    80. S.isupper() -> bool
    81. Return True if all cased characters in S are uppercase and there is
    82. at least one cased character in S, False otherwise.
    83. """
    84. def join(self, iterable):
    85. """
    86. S.join(iterable) -> str
    87. Return a string which is the concatenation of the strings in the
    88. iterable. The separator between elements is S.
    89. """
    90. >>> n = ['alex','jack','rain']
    91. >>> '|'.join(n)
    92. 'alex|jack|rain'
    93. def ljust(self, width, fillchar=None):
    94. """
    95. S.ljust(width[, fillchar]) -> str
    96. Return S left-justified in a Unicode string of length width. Padding is
    97. done using the specified fill character (default is a space).
    98. """
    99. return ""
    100. def lower(self):
    101. """
    102. S.lower() -> str
    103. Return a copy of the string S converted to lowercase.
    104. """
    105. return ""
    106. def lstrip(self, chars=None):
    107. """
    108. S.lstrip([chars]) -> str
    109. Return a copy of the string S with leading whitespace removed.
    110. If chars is given and not None, remove characters in chars instead.
    111. """
    112. return ""
    113. def replace(self, old, new, count=None):
    114. """
    115. S.replace(old, new[, count]) -> str
    116. Return a copy of S with all occurrences of substring
    117. old replaced by new. If the optional argument count is
    118. given, only the first count occurrences are replaced.
    119. """
    120. return ""
    121. def rjust(self, width, fillchar=None):
    122. """
    123. S.rjust(width[, fillchar]) -> str
    124. Return S right-justified in a string of length width. Padding is
    125. done using the specified fill character (default is a space).
    126. """
    127. return ""
    128. def rsplit(self, sep=None, maxsplit=-1):
    129. """
    130. S.rsplit(sep=None, maxsplit=-1) -> list of strings
    131. Return a list of the words in S, using sep as the
    132. delimiter string, starting at the end of the string and
    133. working to the front. If maxsplit is given, at most maxsplit
    134. splits are done. If sep is not specified, any whitespace string
    135. is a separator.
    136. """
    137. return []
    138. def rstrip(self, chars=None):
    139. """
    140. S.rstrip([chars]) -> str
    141. Return a copy of the string S with trailing whitespace removed.
    142. If chars is given and not None, remove characters in chars instead.
    143. """
    144. return ""
    145. def split(self, sep=None, maxsplit=-1):
    146. """
    147. S.split(sep=None, maxsplit=-1) -> list of strings
    148. Return a list of the words in S, using sep as the
    149. delimiter string. If maxsplit is given, at most maxsplit
    150. splits are done. If sep is not specified or is None, any
    151. whitespace string is a separator and empty strings are
    152. removed from the result.
    153. """
    154. return []
    155. def startswith(self, prefix, start=None, end=None):
    156. """
    157. S.startswith(prefix[, start[, end]]) -> bool
    158. Return True if S starts with the specified prefix, False otherwise.
    159. With optional start, test S beginning at that position.
    160. With optional end, stop comparing S at that position.
    161. prefix can also be a tuple of strings to try.
    162. """
    163. return False
    164. def strip(self, chars=None):
    165. """
    166. S.strip([chars]) -> str
    167. Return a copy of the string S with leading and trailing
    168. whitespace removed.
    169. If chars is given and not None, remove characters in chars instead.
    170. """
    171. return ""
    172. def swapcase(self):
    173. """
    174. S.swapcase() -> str
    175. Return a copy of S with uppercase characters converted to lowercase
    176. and vice versa.
    177. """
    178. return ""
    179. def upper(self):
    180. """
    181. S.upper() -> str
    182. Return a copy of S converted to uppercase.
    183. """
    184. return ""
    185. def zfill(self, width):
    186. """
    187. S.zfill(width) -> str
    188. Pad a numeric string S with zeros on the left, to fill a field
    189. of the specified width. The string S is never truncated.
    190. """
    191. return ""
posted @ 2021-03-16 11:33  好吗,好  阅读(54)  评论(0)    收藏  举报