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

上一篇 / 下一篇  2022-03-16 11:34:00

  16、isupper()
  判断字符串中的所有字母是否都为大写,是则返回True,否则返回False。
  print('SIMPLE IS BETTER THAN COMPLEX'.isupper()) # True
  print('SIMPLE IS BETTER THAN complex'.isupper()) # False

  17、isalpha()
  如果字符串至少有一个字符并且所有字符都是字母,则返回 True,否则返回 False。
  s = 'python'
  print(s.isalpha())
  # True
  s = '123'
  print(s.isalpha())
  # False
  s = 'python123'
  print(s.isalpha())
  # False
  s = 'python-123'
  print(s.isalpha())
  # False

  18、isnumeric()
  如果字符串中只包含数字字符,则返回 True,否则返回 False。
  s = 'python'
  print(s.isnumeric())
  # False
  s = '123'
  print(s.isnumeric())
  # True
  s = 'python123'
  print(s.isnumeric())
  # False
  s = 'python-123'
  print(s.isnumeric())
  # False

  19、isalnum()
  如果字符串中至少有一个字符并且所有字符都是字母或数字,则返回True,否则返回 False。
  s = 'python'
  print(s.isalnum())
  # True
  s = '123'
  print(s.isalnum())
  # True
  s = 'python123'
  print(s.isalnum())
  # True
  s = 'python-123'
  print(s.isalnum())
  # False

  20、count()
  返回指定内容在字符串中出现的次数。
  n = 'hello world'.count('o')
  print(n)
  # 2
  n = 'hello world'.count('oo')
  print(n)
  # 0

  21、find()
  检测指定内容是否包含在字符串中,如果是返回开始的索引值,否则返回-1。
  s = 'Machine Learning'
  idx = s.find('a')
  print(idx)
  print(s[idx:])
  # 1
  # achine Learning
  s = 'Machine Learning'
  idx = s.find('aa')
  print(idx)
  print(s[idx:])
  # -1
  # g

  此外,还可以指定开始的范围。
  s = 'Machine Learning'
  idx = s.find('a', 2)
  print(idx)
  print(s[idx:])
  # 10
  # arning

  22、rfind()
  类似于find()函数,返回字符串最后一次出现的位置,如果没有匹配项则返回 -1。
  s = 'Machine Learning'
  idx = s.rfind('a')
  print(idx)
  print(s[idx:])
  # 10
  # arning

  23、startswith()
  检查字符串是否是以指定内容开头,是则返回 True,否则返回 False。
  print('Patrick'.startswith('P'))
  # True

  24、endswith()
  检查字符串是否是以指定内容结束,是则返回 True,否则返回 False。
  print('Patrick'.endswith('ck'))
  # True

  25、partition()
  string.partition(str),有点像find()和split()的结合体。
  从str出现的第一个位置起,把字符串string分成一个3 元素的元组(string_pre_str,str,string_post_str),如果string中不包含str则 string_pre_str==string。
  s = 'Python is awesome!'
  parts = s.partition('is')
  print(parts)
  # ('Python ', 'is', ' awesome!')
  s = 'Python is awesome!'
  parts = s.partition('was')
  print(parts)
  # ('Python is awesome!', '', '')

  26、center()
  返回一个原字符串居中,并使用空格填充至长度width的新字符串。
  s = 'Python is awesome!'
  s = s.center(30, '-')
  print(s)
  # ------Python is awesome!------

  27、ljust()
  返回一个原字符串左对齐,并使用空格填充至长度width的新字符串。
  s = 'Python is awesome!'
  s = s.ljust(30, '-')
  print(s)
  # Python is awesome!------------

  28、rjust()
  返回一个原字符串右对齐,并使用空格填充至长度width的新字符串。
  s = 'Python is awesome!'
  s = s.rjust(30, '-')
  print(s)
  # ------------Python is awesome!

  29、f-Strings
  f-string是格式化字符串的新语法。
  与其他格式化方式相比,它们不仅更易读,更简洁,不易出错,而且速度更快!
  num = 1
  language = 'Python'
  s = f'{language} is the number {num} in programming!'
  print(s)
  # Python is the number 1 in programming!
  num = 1
  language = 'Python'
  s = f'{language} is the number {num*8} in programming!'
  print(s)
  # Python is the number 8 in programming!

  30、swapcase()
  翻转字符串中的字母大小写。
  s = 'HELLO world'
  s = s.swapcase()
  print(s)
  # hello WORLD

  31、zfill()
  string.zfill(width)。
  返回长度为width的字符串,原字符串string右对齐,前面填充0。
  s = '42'.zfill(5)
  print(s)
  # 00042
  s = '-42'.zfill(5)
  print(s)
  # -0042
  s = '+42'.zfill(5)
  print(s)
  # +0042


TAG: 软件开发 Python

 

评分:0

我来说两句

Open Toolbar