字符串内置方法大全

方法描述代码
capitalize第一个字符大写,其余小写
>>> str='this is a dog lovely'
>>> str.capitalize()
'This is a dog lovely'

 

casefold返回全小写
>>> str='This is a lovely dog'
>>> str.casefold()
'this is a lovely dog'

 

center(width)居中字符串,并使用空格填充长度为width的新字符串
>>> str='This is a lovely dog'
>>> str.center(30)
'     This is a lovely dog     '

 

count(sub,start,end)含有sub字符长的次数,start,和end为查找范围,不写默认为整个字符串
>>> str='This is a lovely dog'
>>> str.count('o',0,12)
1

 

encode字符串编码转换,默认为 encoding=‘utf-8’ 
endswith(sub)字符串是否以sub为结尾,返回True或者False
>>> str='This is a lovely dog'

>>> str.endswith('dog')

True

 

expandtabs(tabsize)
把字符串中tab符号(\t)转化为空格,不写tabsize,默认tabsize=8,从前一个子字符串首字符算起,相距后一个子字符串首字符为tabsize
>>> str='This\tis\ta\tlovely\tdog'
>>> str.expandtabs()
'This    is      a       lovely  dog'

 

find(sub[start,end])在一个较长的字符串里查找字串,返回子串所在位置最左端索引,如果没有找到则返回-1
>>> str='This is a lovely dog'
>>> str.find('o',0,20)
11

 

format字符串格式化的一种方法,将需要格式化的地方填充 {}
>>> str='{} is a lovely dog'
>>> str.format('this')
'this is a lovely dog'

 

format_map字符串格式化的一种方法,接收的参数为字典,通过映射的方式格式化
>>> str='{this} is a lovely dog'
>>> a={'this':'that'}
>>> str.format_map(a)
'that is a lovely dog'

 

index(sub,start,end)与find一样,不过sub不在字符串中会出现异常

>>> str='This is a lovely dog'
>>> str.index('is',0,10)
2

 

isalnum至少有一个字符并且所有字符都是字母或数字,则返回True,否则返回False
>>> str='123lovely'
>>> str.isalnum()
True
>>> str='123 lovely'
>>> str.isalnum()
False

 

isalpha至少有一个字符并且所有字符都是字母,则返回True,否则返回False
>>> str='123lovely'
>>> str.isalpha()
False
>>> str='lovely'
>>> str.isalpha()
True
isdecimal如果字符串只包含十进制数字,返回True,否则返回False
>>> str='123'
>>> str.isdecimal()
True
isdigit如果字符串只包含数字,返回True,否则返回False
>>> str='123'
>>> str.isdigit()
True
isidentifier判断字符串是否为有效定义
>>> str=' '
>>> str.isidentifier()
False
>>> str='q'
>>> str.isidentifier()
True
islower如果字符串中至少包含一个区分大小写的字母,并且全是小写,返回True,否则返回Flase
>>> str='Lovely'
>>> str.islower()
False
isnumeric如果字符串只包含数字字符,返回True,否则返回False
>>> str='123lovely'
>>> str.isnumeric()
False

 

isprintable判断字符串中所有字符是否都是可打印字符(in repr())或字符串为空
>>> str=' '
>>> str.isprintable()
True
istitle如果字符串是标题化(所有单词首字母为大写字母,其余小写),返回True,否则返回false
>>> str='This Is A Lovely Dog'
>>> str.istitle()
True

 

isupper如果字符串中至少包含一个区分大小写的字母,并且全是大写,返回True,否则返回Flase
>>> str='This Is A Lovely Dog'
>>> str.isupper()
False
join(sub)以字符串作为分隔符,插入到sub中所有的字符之间
>>> str='This Is A Lovely Dog'
>>> str.join('123')
'1This Is A Lovely Dog2This Is A Lovely Dog3'

 

ljust(width)返回左对齐的字符串,并使用空格填充至长度为width的新字符串
>>> str='This Is A Lovely Dog'
>>> str.ljust(30)
'This Is A Lovely Dog          '
lower把字符串中所有大写转化为小写
>>> str='This Is A Lovely Dog'
>>> str.lower()
'this is a lovely dog'
lstrip去掉字符串左边所有空格
>>> str='  123lovely'
>>> str.lstrip()
'123lovely'
maketrans返回一个用translate的转换表,示例中 ste与 abc为映射关系,每个字符相对应(s->a,t->b,e->c), outtab用于替换 intab出现在需要处理的字符串中,第三个参数为需要删除的字符
​
>>> intab ='st'
>>> outtab="#$"
>>> deltab='_'
>>> str='This is a lovely dog'
>>> str=str.maketrans(intab,outtab,deltab)
>>> str
{115: 35, 116: 36, 95: None}

​
partition(sub)找到字符串sub,并把字符串分为三个元组(pre_sub,sub,fol_sub) ,如果字符串中不包含则返回(“原字符串”," “,” ")
>>> str='This is a lovely dog'
>>> str.partition('a')
('This is ', 'a', ' lovely dog')
>>> str.partition('b')
('This is a lovely dog', '', '')
replace(old,new,count)
把字符串中old字符转化为new字符,如果count填充,则替换不超过count次
>>> str='This is a lovely dog'
>>> str.replace('i','a',2)
'Thas as a lovely dog'
>>> str.replace('is','at',2)
'That at a lovely dog'
>>> str.replace('is','at',1)
'That is a lovely dog'
rfind(sub,start,end)类似于find方法,不过是从右边查找
>>> str='This is a lovely dog'
>>> str.rfind('o',0,3)
-1
>>> str.rfind('o',0,10)
-1
rindex类似与index方法,找到返回最大的索引
>>> str='This is a lovely dog'
>>> str.rindex('is')
5
rjust返回一个右对齐的字符串,并用width填充至为width的新字符串
>>> str='This is a lovely dog'
>>> str.rjust(30)
'          This is a lovely dog'
rpartition类似于partition方法,不过是从右边查找
>>> str='This is a lovely dog'
>>> str.rpartition('o')
('This is a lovely d', 'o', 'g')
rsplit(sub,maxsplit)从右向左寻找,以某个元素为中心将左右分割成两个元素并放入列表中,maxsplit参数默认为-1,表示根据指定分割字符全部分割, maxsplit=1即根据指定分割字符仅分割一次,使用效果和rpartition类似
>>> str='This is a lovely dog'
>>> str.split('o')
['This is a l', 'vely d', 'g']
>>> str.split('o',1)
['This is a l', 'vely dog']
rstrip删除字符串末尾空格,回车,制表符等
>>> str='This is a lovely dog   '
>>> str.rstrip()
'This is a lovely dog'
split(sep=None,maxsplit=-1)从左向右寻找,以某个元素为中心将左右分割成两个元素并放入列表中,maxsplit参数默认为-1,表示根据指定分割字符全部分割, maxsplit=1即根据指定分割字符仅分割一次,使用效果和partition类似
>>> str='This is a lovely dog'
>>> str.split('o',-1)
['This is a l', 'vely d', 'g']
>>> str.split('o',1)
['This is a l', 'vely dog']
splitlines(keepends)按照’\n’分隔,返回一个包含各行作为元素的列表(’\r’, ‘\r\n’, \n’),如果keepends指定,则返回前keepends行
>>> str='''It's a nice day.
with beautiful mood.
and you
'''
>>> str.splitlines(True)
["It's a nice day.\n", 'with beautiful mood.\n', 'and you\n']
startswith(prefix,start,end)检查字符串是否以prefix开头,是则返回True,否则返回False
>>> str='This is a lovely dog'
>>> str.startswith('This')
True
strip删除字符串左右两边的空格,回车,制表符等
>>> str='  This is a lovely dog   '
>>> str.strip()
'This is a lovely dog'
swapcase将字符串中大小写字符翻转为小大写字符
>>> str='This is a lovely dog'
>>> str.swapcase()
'tHIS IS A LOVELY DOG
title()将字符串转为标题字符串,即每个单词首字母大写
>>> str='This is a lovely dog'
>>> str.title()
'This Is A Lovely Dog'
translate(table)根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 del 参数中。table表是通过maketrans方法转换而来
>>> intab ='st'
>>> outab="#$"
>>> deltab='_'
>>> str='This is a lovely dog'
>>> str=str.maketrans(intab,outab,deltab)
>>> str.translate(str)
'This is a lovely dog'
upper()把字符串中所有小写转化为大写
>>> str='This is a lovely dog'
>>> str.upper()
'THIS IS A LOVELY DOG'
zfill(width)返回长度为width字符串,用0填充
>>> str='This is a lovely dog'
>>> str.zfill(30)
'0000000000This is a lovely dog'
   

 

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值