31个必备的Python字符串方法,建议收藏!(一)

上一篇 / 下一篇  2022-03-16 11:22:14

  字符串是Python中基本的数据类型,几乎在每个Python程序中都会使用到它。
  1、Slicing
  slicing切片,按照一定条件从列表或者元组中取出部分元素(比如特定范围、索引、分割值)
  s = '   hello   '
  s = s[:]
  print(s)
  #    hello
  s = '   hello   '
  s = s[3:8]
  print(s)
  # hello

  2、strip()
  s = '   hello   '.strip()
  print(s)
  # hello
  s = '###hello###'.strip()
  print(s)
  # ###hello###

  在使用strip()方法时,默认去除空格或换行符,所以#号并没有去除。
  可以给strip()方法添加指定字符,如下所示。
  s = '###hello###'.strip('#')
  print(s)
  # hello

  此外当指定内容不在头尾处时,并不会被去除。
  s = ' \n \t hello\n'.strip('\n')
  print(s)
  #
  #      hello
  s = '\n \t hello\n'.strip('\n')
  print(s)
  #      hello

  第一个\n前有个空格,所以只会去取尾部的换行符。
  最后strip()方法的参数是剥离其值的所有组合,这个可以看下面这个案例。
  s = 'www.baidu.com'.strip('cmow.')
  print(s)
  # baidu

  最外层的首字符和尾字符参数值将从字符串中剥离。字符从前端移除,直到到达一个不包含在字符集中的字符串字符为止。
  在尾部也会发生类似的动作。
  3、lstrip()
  移除字符串左侧指定的字符(默认为空格或换行符)或字符序列。
  s = '   hello   '.lstrip()
  print(s)
  # hello

  同样的,可以移除左侧所有包含在字符集中的字符串。
  s = 'Arthur: three!'.lstrip('Arthur: ')
  print(s)
  # ee!

  4、rstrip()
  移除字符串右侧指定的字符(默认为空格或换行符)或字符序列。
  s = '   hello   '.rstrip()
  print(s)
  #    hello

  5、removeprefix()
  Python3.9中移除前缀的函数。
  # python 3.9
  s = 'Arthur: three!'.removeprefix('Arthur: ')
  print(s)
  # three!

  和strip()相比,并不会把字符集中的字符串进行逐个匹配。
  6、removesuffix()
  Python3.9中移除后缀的函数。
  s = 'HelloPython'.removesuffix('Python')
  print(s)
  # Hello

  7、replace()
  把字符串中的内容替换成指定的内容。
  s = 'string methods in python'.replace(' ', '-')
  print(s)
  # string-methods-in-python
  s = 'string methods in python'.replace(' ', '')
  print(s)
  # stringmethodsinpython

  8、re.sub()
  re是正则的表达式,sub是substitute表示替换。
  re.sub则是相对复杂点的替换。
  import re
  s = "string    methods in python"
  s2 = s.replace(' ', '-')
  print(s2)
  # string----methods-in-python
  s = "string    methods in python"
  s2 = re.sub("\s+", "-", s)
  print(s2)
  # string-methods-in-python

  和replace()做对比,使用re.sub()进行替换操作,确实更高级点。
  9、split()
  对字符串做分隔处理,最终的结果是一个列表。
  s = 'string methods in python'.split()
  print(s)
  # ['string', 'methods', 'in', 'python']

  当不指定分隔符时,默认按空格分隔。
  s = 'string methods in python'.split(',')
  print(s)
  # ['string methods in python']

  此外,还可以指定字符串的分隔次数。
  s = 'string methods in python'.split(' ', maxsplit=1)
  print(s)
  # ['string', 'methods in python']

  10、rsplit()
  从右侧开始对字符串进行分隔。
  s = 'string methods in python'.rsplit(' ', maxsplit=1)
  print(s)
  # ['string methods in', 'python']

  11、join()
  string.join(seq)。以string作为分隔符,将seq中所有的元素(的字符串表示)合并为一个新的字符串。
  list_of_strings = ['string', 'methods', 'in', 'python']
  s = '-'.join(list_of_strings)
  print(s)
  # string-methods-in-python
  list_of_strings = ['string', 'methods', 'in', 'python']
  s = ' '.join(list_of_strings)
  print(s)
  # string methods in python

  12、upper()
  将字符串中的字母,全部转换为大写。
  s = 'simple is better than complex'.upper()
  print(s)
  # SIMPLE IS BETTER THAN COMPLEX

  13、lower()
  将字符串中的字母,全部转换为小写。
  s = 'SIMPLE IS BETTER THAN COMPLEX'.lower()
  print(s)
  # simple is better than complex

  14、capitalize()
  将字符串中的首个字母转换为大写。
  s = 'simple is better than complex'.capitalize()
  print(s)
  # Simple is better than complex

  15、islower()
  判断字符串中的所有字母是否都为小写,是则返回True,否则返回False。
  print('SIMPLE IS BETTER THAN COMPLEX'.islower()) # False
  print('simple is better than complex'.islower()) # True


TAG: 软件开发 Python

 

评分:0

我来说两句

Open Toolbar