欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python中字符串内置函数的用法总结

程序员文章站 2022-10-23 12:52:43
capitalize() 首字母大写 a='someword' b=a.capitalize() print(b) —>someword...

capitalize() 首字母大写

a='someword' 
 b=a.capitalize() 
 print(b) 
 —>someword

casefold()&lower() 所有字母变小写,casefold可将未知字符便小写

a='someword' 
  b=a.casefold() 
  print(b) 
  c=a.lower() 
  print(c) 
  —>someword 
  —>someword

center(width,fillchar=none) 设置宽度,并将内容居中,空白未知填充,一个字符

a='someword' 
  b=a.center(30,'*') 
  print(b)

count(sub,start=none,end=none) 去字符串中寻找,寻找子序列的出现次数,可指定起止点

a='somewordsomeword' 
 b=a.count(‘or') 
 print(b) 
 —>2

startswith(suffix,start=none,end=none)&endswith(suffix,start=none,end=none) 是否以xx开始/结束,可指定起止点

a='somewordsomeword' 
  b=a.startswith(‘sa') 
  c=a.endswith(‘ord') 
  print(b) 
  print(c) 
  —>false 
  —>true

find(sub,start=none,end=none) 寻找指定字符或字符串,并返回第一个位置,找不到返回-1,可指定起止点

a='somewordsomeword' 
  b=a.find(‘me') 
  print(b) 
  —>2

format() 格式化,将一个字符串中的占位符替换为指定的值

test='i am {name},age {a}' 
  v=test.format(name='alex',a=19) 
  print(v) 
  —>i am alex,age 19

format_map() 格式化,传入的值

test='iam{name},age{a}' 
  v=test.format_map({“name”:'alex',”a”:19}) 
  print(v) 
  —>i am alex,age 19

isalnum() 字符串中是否只包含字母和数字

a='asdfs123*' 
  b=a.isalnum() 
  print(b) 
  —>false

expandtabs(tabsize=number) 将字符串以number分割,并将tab补入

a='asdfs123\t523fgbdf' 
 b=a.expandtabs(5) 
 print(b)
 —>asdfs123 523fgbdf

isalpha() 字符串中是只包含字母

a='asdfsfgbdf' 
 b=a.isalpha() 
 print(b) 
 —>true

isdecimal()&isdigit()&isnumeric() 字符串中是只包含数字,isdigit更为强大,isnumeric还可识别中文

a='132132②二' 
  b=a.isdecimal() 
  c=a.isdigit() 
  d=a.isnumeric() 
  print(b) 
  print(c) 
  print(d) 
  —>false 
  —>false 
  —>true

isprintable() 是否存在不可显示的字符如换行符

a='sdfgdfg\t' 
 b=a.isprintable() 
 print(b) 
 —>false

isspace() 判断是否全部为空格

a='dsvsdv' 
  b=a.isspace() 
  print(b) 
  —>false

istitle()&title() 判断是否为标题,即首字母大写&变为标题

a='follow uncased characters and lowercase characters only cased ones' 
  b=a.istitle() 
  print(b) 
  c=a.title() 
  print(c) 
  —>false 
  —>follow uncased characters and lowercase characters only cased ones

join(iterable) 将字符串中的每个元素按照指定分隔符进行拼接

a='一二三四五六七' 
  print(a) 
  b='*' 
  c=b.join(a) 
  print(c) 
  —>一二三四五六七 
  —>一二三四五六七

ljust(width,fillchar=none)&rjust(width,fillchar=none) 向右/左填充字符

a='hello' 
 b=a.ljust(20,'*') 
 c=a.rjust(20,'*') 
 print(b) 
 print(c) 
 —>hello*************** 
 —>***************hello

islower()&lower() 判断是是否为全小写&变为全部小写

a='hello' 
  b=a.islower() 
  c=a.lower() 
  print(b,c) 
  —>false hello

isupper()&c=a.upper() 判断是是否为全大写&变为全部大写

a='hello' 
  b=a.isupper() 
  c=a.upper() 
  print(b,c) 
  —>false hello

lstrip(chars=none)&rstrip(chars=none)&strip(chars=none) 去除字符串左边/右边/两边的字符串,默认空格,换行等

a='hello' 
  b=a.lstrip() 
  c=a.rstrip() 
  d=a.strip() 
  print(b) 
  print(c) 
  print(d) 
  —>hello 
  —> hello 
  —>hello

maketrans(*args,**kwargs)&translate(table) 按maketrans对应关系将translate中的字符串进行替换

a='asdgfrfbcvzxrentas' 
  b=str.maketrans(‘xdsa','1234') 
  c=a.translate(b) 
  print(c) 
  —> 432gfrfbcvz1rent43

partition(sep)&rpartition(sep) 将字符串按指定字符分割成3段/或从右开始

a='helwloasvxcwaewc' 
  b=a.partition(‘w') 
  c=a.rpartition(‘w') 
  print(b) 
  print(c) 
  —>(‘hel', ‘w', ‘loasvxcwaewc') 
  —>(‘helwloasvxcwae', ‘w', ‘c')

split(sep=none,maxsplit=-1)&rsplit(sep=none,maxsplit=-1) 将字符串按指定字符串分割,分割后不保留

a='helwloasvxcwaewc' 
  b=a.split(‘w',2) 
  c=a.rsplit(‘w') 
  print(b) 
  print(c) 
  —>[‘hel', ‘loasvxc', ‘aewc'] 
  —>[‘hel', ‘loasvxc', ‘ae', ‘c']

splitlines(keepends=none) 按照换行符进行分割,带true参数保留换行符

a='helwloas\nvxcwaewc\nafgasdfs' 
  b=a.splitlines() 
  c=a.splitlines(true) 
  print(b) 
  print(c) 
  —>[‘helwloas', ‘vxcwaewc', ‘afgasdfs'] 
  —>[‘helwloas\n', ‘vxcwaewc\n', ‘afgasdfs']

startswith(prefix,start=none,end=none)&endswith(prefix,start=none,end=none) 判断字符串是否以指定字符开始/结束,可指定起止点

a='aefsfsfeeav' 
  b=a.startswith(‘ae') 
  c=a.endswith(‘av',1,9) 
  print(b) 
  print(c) 
  true 
  —>false

swapcase() 小写转变为大写

a='aefsfsfeeav' 
  b=a.swapcase() 
  print(b) 
  —>aefsfsfeeav