Python--19. 字符串常用操作

7.4 字符串常用操作

  • 字符串对象提供的设计字符串“修改”的方法都是返回修改后的新字符串,并不对原字符串做任何修改

7.4.1 find()、rfind()、index()、rindex()、count()

  • find():查找一个字符串在另一个字符串指定范围(默认是整个字符串)中首次出现的位置,如果不存在 则返回-1.
  • rfind():最后一次出现的位置(其余同上)。
s = 'apple,peach.banana,peach,pear'
print(s.find('peach',7))  #从指定位置开始查找
print(s.find('peach',7,20))
print(s.rfind('peach'))
19
-1
19
  • index():返回一个字符串在另一个字符串指定范围中首次出现的位置,若不存在则抛出异常。
  • rindex():最后一次出现的位置(其余同上)。
print(s.index('pe'))
print(s.index('ppp'))
6



---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-4-31d2d28f4e52> in <module>
      1 print(s.index('pe'))
----> 2 print(s.index('ppp'))


ValueError: substring not found
  • count():返回一个字符串在另一个字符串中出现的次数,若不存在则返回0。
print(s.count('p'))
print(s.count('ppp'))
5
0

7.4.2 split()、rsplit()

  • split():从左端开始。

  • split():从右端开始。

  • 语法:str.split(str="",num=string.count(str))[n]

  • 参数说明:

    • str:表示为分隔符,默认为空格,但是不能为空(’’)。若字符串中没有分隔符,则把整个字符串作为列表的一个元素。
    • num:分割次数。默认为 -1, 即分隔所有。
    • [n]:表示选取第n个分片。
s = 'apple,peach.banana,pear'
print(s.split(','))
print(s.rsplit(','))
['apple', 'peach.banana', 'pear']
['apple', 'peach.banana', 'pear']
  • 对于split()和rsplit()方法,若不指定分隔符,则字符串中的任何空白符号(空格、换行符、制表符等)的连续出现都将被认为是分隔符,返回包含最终分隔结果的列表。
s = '\n\nhello\t\t world \n\n\n my name\t is Xu'
s.split()
['hello', 'world', 'my', 'name', 'is', 'Xu']
s = '\n\nhello\t\t world \n\n\n my name\t is Xu'
print(s.split(' ',2))
print(s.split(maxsplit = 2))
print(s.rsplit(maxsplit = 2))
['\n\nhello\t\t', 'world', '\n\n\n my name\t is Xu']
['hello', 'world', 'my name\t is Xu']
['\n\nhello\t\t world \n\n\n my name', 'is', 'Xu']

7.4.3 join()

  • 将列表中多个字符串进行连接,并在相邻两个字符串之间插入指定字符,返回新字符串。
li = ['apple','peach','banana','pear']
sep = ','
print(sep.join(li))
print(''.join(li))
apple,peach,banana,pear
applepeachbananapear

7.4.4 lower()、upper()、capitalize()、title()、swapcase()

  • str.lower():全部字符小写
  • str.upper():全部字符大写
  • str.capitalize():字符串首字母大写
  • str.title():每个单词的首字母大写
  • str.swapcase():大小写互换
  • 以上均生成新字符串,不对原字符串做任何修改

7.4.5 replace()

  • str.replace(old, new[, max])
    • max:用来替换的次数
      • (1)当不将max参数传入时,默认将所有old字符或者字符串替换为new字符或者字符串
      • (2)当我们将max参数传入后,则将旧字符串替换为新字符串不超过max次,多余的则不进行替换
  • 返回一个新字符串

7.4.6 strip()、rstrip()、lstrip()

  • str.strip([chars])
    • chars:移除字符串头尾指定的字符。

7.4.7 startswith()、endswith()

  • str.startswith(suffix[, start[, end]]) str[start:end]以suffix开始返回True,否则返回False
  • str.endswith(suffix[,start[,end]]) str[start:end]以suffix结尾返回True,否则返回False

7.4.8

  • str.islower():是否为小写字母
  • str.isupper():是否为大写字母
  • str.isprintable() 当str所有字符都是可打印的,返回True,否则False
  • str.isdigit():是否为数字字符
  • str.isnumeric():是否为数字字符
  • str.isspace():是否为空白字符
  • str.isalnum()是否为数字或字母
  • str.isalpha():是否为字母

center()、ljust()、rjust()、zfill()

s = 'Hello world!'
print(s.center(20))  #居中对齐,以空格进行填充
print(s.center(20,'='))  #居中对齐,以字符=进行填充
print(s.ljust(20,'='))  #左对齐
print(s.rjust(20,'='))  #右对齐
print('abc'.zfill(5))  #在左侧填充数字字符0
print('abc'.zfill(2))  #指定宽度小于字符串长度时,返回字符串本身
    Hello world!    
====Hello world!====
Hello world!========
========Hello world!
00abc
abc
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值