Python常用的str方法

常用str的方法

首先来看如何查看一个序列的方法,python中自带的dir函数可以查看,例如要查看字符串的方法

print(dir(' '))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

方法有很多,这里只提及常用的方法,对于不常用的方法,遇到时查看即可。

index(find):查找某一个字符的起始位置,若找不到,index报错,find返回假

count:返回一个字符串中要寻找的字符的个数。

isdigit:判断字符串是否只有数字,返回值为True或者False

isalpha:判断字符串是否只有字母,返回值为True或者False

endswith:判断是否以某个值为结束,返回值为True或者False

startswith:判断是否以某个值为开始,返回值为True或者False

islower:判断是否只有小写,返回值为True或者False,出现除英文字符外的字符对返回值没有影响,但是若没有英文字符,返回值均为False

isupper:判断是否只有大写,返回值为True或者False

a.capitalize():首字母大写

a.title():单词首字母大写

upper:所有英文字符串转换为大写

lower:所有英文字符串转换为小写

strip:消除字符串前后的所有空格,中间的去不掉(lstrip只取消掉字符串左边的空格,rstrip只取消字符串右边的空格)

split:根据某一个字符分割字符串,返回列表,字符切掉

index和find方法

#首先来看index和find这二者稍有区别,当所要查找的字符在字符串当中时,二者是一样的,均返回元素的索引值
a = 'abcdef'
print(a.index('b'))
print(a.find('b'))
1
1
#当所要查找的字符并不在字符串当中时,这两种方法的返回值是不一样的
a = 'abcdef'
print(a.find('g'))
-1

返回值为-1,表示false没有找到,而使用index方法,当要查找的字符不在字符串中时,系统报错

print(a.index('g'))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-28-5b5ab181b8f2> in <module>
----> 1 print(a.index('g'))

ValueError: substring not found

count方法

#返回字符串中所要查找的字符的个数
a = 'aaabbbc'
print(a.count('a'))
3

isdigit方法

#isdigit判断一个字符串是否是纯数字,若为纯数字返回True否则返回False
a = '1234'
b = '12asd'
print(a.isdigit())
print(b.isdigit())
True
False

isalpha方法

#isalpha判断一个字符串是否是纯字母,若为纯数字返回True否则返回False
a = '1234hu'
b = 'abfDFG'
print(a.isalpha())
print(b.isalpha())
False
True

endswith方法

#endswith判断一个字符串是否是以某一个字符结尾,若是返回True否则返回False
a = '1232435'
print(a.endswith('5'))
print(a.endswith('6'))
True
False

startswith方法

#startswith判断一个字符串是否是以某一个字符开始,若是返回True否则返回False
a = '1232435'
print(a.startswith('1'))
print(a.startswith('2'))
True
False

islower方法

#islower:判断是否只有小写,返回值为True或者False,出现除英文字符外的字符对返回值没有影响,但是若没有英文字符,返回值均为False
a = 'abcdeF'
b = 'abcdeF213'
c = '123'
d = 'adad'
print(a.islower())
print(b.islower())
print(c.islower())
print(d.islower())
False
False
False
True

isupper方法

#isupper:判断是否只有小写,返回值为True或者False,出现除英文字符外的字符对返回值没有影响,但是若没有英文字符,返回值均为False
a = 'aGFHAGJF'
b = 'GFHAGJF124'
c = '123'
d = 'GFHAGJF'
print(a.isupper())
print(b.isupper())
print(c.isupper())
print(d.isupper())
False
True
False
True

capitalize方法

#capitalize:字符串首字母大写,若字符串首字母为小写字母转化为大写
a = 'hello world!'
print(a.capitalize())
Hello world!

title方法

#title:单词首字母大写
a = 'hello world!'
print(a.title())
Hello World!

upper方法

#upper:所有英文字符串转换为大写
a = 'hello world!'
print(a.upper())
HELLO WORLD!

lower方法

#lower方法:所有英文字符串转换为小写
a = 'HELLO WORLD!'
print(a.lower())
hello world!

strip方法

#strip:消除字符串前后的所有空格,中间的去不掉(lstrip只取消掉字符串左边的空格,rstrip只取消字符串右边的空格)
a = '     hello world!      '
print(a.lstrip())
print(a.rstrip())
print(a.strip())
hello world!      
     hello world!
hello world!

split方法

#split:根据某一个字符分割字符串,返回列表,字符切掉
a = 'hello,world,python'
print(a.split(','))
['hello', 'world', 'python']

replace方法

#replace('需要替换的目标字符串','替换的新字符串',替换个数)
#第三个参数用来说明修改几个字符串,若缺省则默认修改全部字符串
a = 'hello,world,python'
print(a.replace('o','高',2))
print(a.replace('o','高'))
hell高,w高rld,python
hell高,w高rld,pyth高n

format方法

#format方法用来拼接字符串
print('{}{}{}'.format(1,2,3))
123
print('{}{}{}'.format([1,545,65],2,3))
[1, 545, 65]23
print('{2}{0[2]}{1}'.format(['wo','ai','ni'],'world','python'))
pythonniworld

join方法

#join:用于将序列中的元素以指定的字符连接生成一个新的字符串。
a = ('hello','world')
b = '-'
print(b.join(a))
hello-world
a = 'hello world'
b = '--'
print(a.join(b))
-hello world-

字符串拼接

a = '123'
b = '456'
print(a+b)
123456
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值