青少年叛逆
青少年叛逆
IT培训
广告招商

Python必备的字符串方法!老男孩Python培训学校

  上一篇文章为大家介绍了16个Python必备的字符串方法,但由于内容有限,小编特将内容分开发布,跟着小编继续往下看吧。

  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

  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()的结合体。

  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、ijust()

  返回一个原字符串左对齐,并使用空格填充至长度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

  老男孩教育是Python培训领域的专家,2012年就开展了Python培训,是行业较早的Python培训机构,积累了大量的Python培训教学经验,并能全局把控企业用人指标,科学的制定Python教学课程体系,满足5-8年职业生涯需求,让学员轻松拿下高薪职位!

注意:吐槽知乎网只是一个问答与文章免费发布平台,所有信息均有会员免费发布,不产生金钱交易,如果你有资金往来,请及时通过电话与对方联系,调查清楚,确认无误在选择,否则造成你的损失,由自己承担,本平台概不负责,谢谢!

0 个评论

要回复文章请先登录注册

狗粮批发
嘿,进入会员专区!