python3 字符串 常用方法

1. find

  • find方法可以在一个较长的字符串中查找 子字符串。它返回子串所在位置的最左端索引。如果没有找到则返回 -1
    # 不添加 起始位置 和 结束位置
    In [1]: test_str = 'python itcast hello'
    In [2]: test_str.find('hello')
    Out[2]: 14
    
      # 添加 起始位置 和 结束位置   注意:左闭右开
      In [1]: test_str = 'python itcast hello'
      In [2]: test_str.find('on',1,11)
      Out[2]: 4
    
      In [3]: test_str.find('on',1,3)
      Out[3]: -1
    

2. rfind

  • 和 find类似,只不过是 从右往左 查找。

3. index

  • 同 find一样,不同的是,如果没有找到则返回ValueError异常。
  • 从右往左 查找 使用 rindex 方法。

3. count

  • 在一个较长的字符串中 查找 子字符串 出现的次数,并且返回这个数值。
      In [1]: test_str = 'python itcast itcast'
      In [2]: test_str.count('itcast')
      Out[2]: 2
    
      和find方法类似,也可以指定 起始位置 和 结束位置:
    
      In [1]: test_str = 'python itcast itcast'
      In [2]: test_str.count('itcast',5,13)
      Out[2]: 1
    

4. replace

  • 把 前面的 字符串 替换成 后面的,返回值为替换后的字符串,不修改原来的字符串。
      In [1]: test_str = 'python itcast itcast'
      In [2]: test_str.replace('itcast','123',1)
      Out[2]: 'python 123 itcast'
    
  • 最后一个参数,代表最多替换的个数,如果不写,默认 全部替换。

5. expandtabs

  • 把字符串中的 tab 符号('\t')转为空格,默认 一个tab 8个空格。
      In [31]: a = "this is\tstring example....wow!!!"
    
      In [32]: a.expandtabs()
      Out[32]: 'this is string example....wow!!!'
    
      说好的 8个空格呢?因为 this is 占了7个 空格。
    
      接着看:
      In [33]: a = "this is\n\tstring example....wow!!!"
      In [34]: a.expandtabs()
      Out[34]: 'this is\n        string example....wow!!!'
      
      如果 \t 前面是 换行符,则 \t 就是 8个空格。
    

6. split

  • 把一个长字符串 按照某些字符来 分隔,返回值为 分隔后的列表,不修改原来的字符串。
    In [1]: test_str = 'python itcast itcast'
    In [2]: test_str.split('it')
    Out[2]: ['python ', 'cast ', 'cast']
    
    然后 看下面:
    In [1]: test_str = 'python itcast itcast'
    In [2]: test_str.split(' ',1)
    Out[2]: ['python', 'itcast itcast']
    
    参数 1 代表 在满足要求的基础上 “切1刀”
    

7. join

  • 用于将序列中的元素以指定的字符(可以是多个字符)连接生成一个新的字符串.
    In [1]: test_list = ['a','b','c']
    In [2]: '-'.join(test_list)
    Out[2]: 'a-b-c' 
    

8. capitalize

  • 把字符串的 第一个字符 大写,返回大写后的字符串,不修改原来的字符串。
    In [1]: test_str = 'python itcast itcast'
    In [2]: test_str.capitalize()
    Out[2]: 'Python itcast itcast'
    

9. title

  • 字符串 每个单词的 首字母 大写,返回大写后的字符串,不修改原来的字符串。
    In [1]: test_str = 'python itcast itcast'
    In [2]: test_str.title()
    Out[2]: 'Python Itcast Itcast'
    

10. startswith

  • 检查字符串是否是以 某个字符串 开头, 是则返回 True,否则返回 False
    In [1]: test_str = 'python itcast itcast'
    In [2]: test_str.startswith('py')
    Out[2]: True
    
    添加 起始位置 和 结束位置:
    In [3]: test_str.startswith('python',0,2)
    Out[3]: False
    
    参数可以是一个元组,  
    In [4]: test_str.startswith(('hh','th'))
    Out[4]: False
    
    

11. endswith

  • 用法和startswith 一样 。检查字符串是否是以 某个字符串 结尾, 是则返回 True,否则返回 False

12. lower

  • 把字符串里所有的大写字符转换为小写,返回值为修改后的字符串,不修改原来的字符串。
    In [1]: test_str = 'HELLO'
    In [2]: test_str.lower()
    Out[2]: 'hello'
    

13. upper

  • 用法和 lower 一样。把字符串里所有的小写字符转换为大写,返回值为修改后的字符串,不修改原来的字符串。

14. ljust

  • 返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。默认填充空格字符。
    In [1]: test_str = 'helo'
    In [2]: test_str.ljust(10,'-')
    Out[2]: 'helo------'
    

15. rjust

  • 和 ljust 用法一样。
  • 返回一个原字符串右对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。默认填充空格字符。

16. center

  • 和 ljust 用法一样。
  • 返回一个原字符串居中对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。默认填充空格字符。

17. lstrip

  • 截掉字符串 左端 的空格或指定字符。
    In [1]: test_str = '   hello--'
    In [2]: test_str.lstrip()
    Out[2]: 'hello--'
    
    指定字符:
    In [1]: test_str = '   hello--'
    In [2]: test_str.lstrip('-')
    Out[2]: '   hello--'
    

18. rstrip

  • 和 lstrip 用法一样。截掉字符串 右端 的空格或指定字符。

19. strip

  • 和 lstrip 用法一样。截掉字符串 前后两端 的空格或指定字符。

20. partition

  • 根据指定的分隔符将字符串进行分割。
  • 如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
    In [1]: test_str = 'python itcast hello itcast world'
    In [2]: test_str.partition('itcast')
    Out[2]: ('python ', 'itcast', ' hello itcast world')
    

21. rpartition

  • 和partition类似,只不过是从右边开始匹配的。
    In [1]: test_str = 'python itcast hello itcast world'
    In [2]: test_str.rpartition('itcast')
    Out[2]: ('python itcast hello ', 'itcast', ' world')
    

22. splitlines

  • 按照行分隔,返回一个包含各行作为元素的列表。
    In [1]: test_str = '''hello
    ...: world'''
    
    In [2]: test_str
    Out[2]: 'hello\nworld'
    
    In [3]: test_str.splitlines()
    Out[3]: ['hello', 'world']
    
    splitlines 有一个参数 keepends 默认为False,表示不包含换行符。
    

23. isalpha

  • 检测字符串是否只由字母组成。这里所说的字母包含中文字符。
    In [1]: test_str = 'abc'
    In [2]: test_str.isalpha()
    Out[2]: True
    

24. isdigit

  • 检测字符串是否只由数字组成。
    In [1]: test_str = '123'
    In [2]: test_str.isdigit()
    Out[2]: True
    

25. isalnum

  • 检测字符串是否只由空格组成。
    In [1]: test_str = '   '
    In [2]: test_str.isspace()
    Out[2]: True
    

26. islower

  • 判断字符串是否是全小写字母组成

27. isupper

  • 判断字符串是否全大写

28. istilte

  • 判断字符串是否是满足所有单词首字母大写格式

29. swapcase

  • 字符串中字母大写转小写,小写转大写

30. zfill

  • 使用0在原始字符串左侧补充到长度为指定值,小数点占1位
    In [40]: a = "3.14"
    In [42]: a.zfill(6)
    Out[42]: '003.14'
    

最后:字符串是不可变类型,所以以上操作都不会修改原字符串。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。