Python中字符串的方法详解
| 1,capitalize(self, /) 返回一个 首字母大写,其余所有字母小写的新字符串
| Return a capitalized version of the string.
|
| More specifically, make the first character have upper case and the rest lower
| case.
| 2,casefold(self, /) 返回一个将所有字母转换为小写的字符串
| Return a version of the string suitable for caseless comparisons.
| 3,center(self, width, fillchar=’ ', /) 返回一个 居中的新字符串,可以自行设置宽度和填充字符
| Return a centered string of length width.
|
| Padding is done using the specified fill character (default is a space).
| 4,count(…) 计算字符串中所需元素出现的次数,可以选择字符串索引的起始位置和结束位置。
| S.count(sub[, start[, end]]) -> int
|
| Return the number of non-overlapping occurrences of substring sub in
| string S[start:end]. Optional arguments start and end are
| interpreted as in slice notation.
| 5,encode(self, /, encoding=‘utf-8’, errors=‘strict’) 以指定的编码格式编码字符串,默认编码为 ‘utf-8’
| Encode the string using the codec registered for encoding.
|
| encoding
| The encoding in which to encode the string.
| errors
| The error handling scheme to use for encoding errors.
| The default is ‘strict’ meaning that encoding errors raise a
| UnicodeEncodeError. Other possible values are ‘ignore’, ‘replace’ and
| ‘xmlcharrefreplace’ as well as any other name registered with
| codecs.register_error that can handle UnicodeEncodeErrors.
| 6,endswith(…) 判断字符串是否以指定字符作为结尾,返回值为bool类型
| S.endswith(suffix[, start[, end]]) -> bool
|
| Return True if S ends with the specified suffix, False otherwise.
| With optional start, test S beginning at that position.
| With optional end, stop comparing S at that position.
| suffix can also be a tuple of strings to try.
| [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QWYJLuYv-1655865307372)(C:\Users\chendafa\AppData\Roaming\Typora\typora-user-images\image-20220620182758965.png)]
| 7,expandtabs(self, /, tabsize=8) 将字符串S中的 \t 替换为一定数量的空格。默认N=8
| Return a copy where all tab characters are expanded using spaces.
|
| If tabsize is not given, a tab size of 8 characters is assumed.
| [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-F89t31F1-1655865307374)(C:/Users/chendafa/AppData/Roaming/Typora/typora-user-images/image-20220622094853290.png)\
| 8, find(…) 查找字符串中指定的子字符串sub第一次出现的位置,可以规定字符串的索引查找范围。若无则返回 -1。
| S.find(sub[, start[, end]]) -> int
|
| Return the lowest index in S where substring sub is found,
| such that sub is contained within S[start:end]. Optional
| arguments start and end are interpreted as in slice notation.
|
| Return -1 on failure.
|
| 9,format(…) 格式化
| S.format(*args, **kwargs) -> str
|
| Return a formatted version of S, using substitutions from args and kwargs.
| The substitutions are identified by braces (‘{’ and ‘}’).
|
| format_map(…)
| S.format_map(mapping) -> str
|
| Return a formatted version of S, using substitutions from mapping.
| The substitutions are identified by braces (‘{’ and ‘}’).
|
| 10,index(…) 查找字符串中第一次出现的子字符串的位置,可以规定字符串的索引查找范围[star,end)。若无则会报错。
| S.index(sub[, start[, end]]) -> int
|
| Return the lowest index in S where substring sub is found,
| such that sub is contained within S[start:end]. Optional
| arguments start and end are interpreted as in slice notation.
|
| Raises ValueError wh