Python中字符串的方法详解

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.

|  [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-afqOeApE-1655865307366)(C:\Users\chendafa\AppData\Roaming\Typora\typora-user-images\image-20220620181130566.png)]

| 2,casefold(self, /) 返回一个将所有字母转换为小写的字符串

| Return a version of the string suitable for caseless comparisons.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YEYMhpuN-1655865493701)(C:\Users\chendafa\AppData\Roaming\Typora\typora-user-images\image-20220620181454843.png)]

| 3,center(self, width, fillchar=’ ', /) 返回一个 居中的新字符串,可以自行设置宽度和填充字符

| Return a centered string of length width.
|
| Padding is done using the specified fill character (default is a space).
|  [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mbu37YLp-1655865307369)(C:\Users\chendafa\AppData\Roaming\Typora\typora-user-images\image-20220620181809145.png)]

| 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.

| [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ar3GYwBN-1655865307370)(C:\Users\chendafa\AppData\Roaming\Typora\typora-user-images\image-20220620182327678.png)]

| 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.
|  [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gjZRsl7z-1655865307371)(C:\Users\chendafa\AppData\Roaming\Typora\typora-user-images\image-20220620182611524.png)]

| 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.
|

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RWniJSEX-1655865307375)(C:\Users\chendafa\AppData\Roaming\Typora\typora-user-images\image-20220620193038555.png)]

| 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

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Royyic

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值