Python字符串需要掌握的方法
文章目录
string.strip()
string.strip() 默认去掉空格,在括号内输入字符就输出去掉字符串两边相应的字符。
string.lstrip() 去掉左边的指定字符
string.rstrip() 去掉右边的指定字符
msg = '***shen****'
print(msg.strip("*"))
print(msg.lstrip("*"))
print(msg.rstrip("*"))
>>>shen
>>>shen****
>>>***shen
string.split()
split()方法可以通过分隔符将一组数据分割成列表,然后再通过列表的索引给参数赋值。(返回值为列表)
split()空格内不填则默认为空格
split(self, __char)
rsplit(self, sep, max) 从字符串的右端开始,只对第一个seq进行切割。
msg = "shen:20:male"
print(msg.split(':'))
>>>['shen', '20', 'male']
msg = "shen:20:male:123"
print(msg.rsplit(':',1))
>>>['shen:20:male', '123']
msg = "shen 20 male"
print(msg.split())
>>>['shen', '20', 'male']
startswith, endswith
msg = "shen:20:male:123"
print(msg.startswith("shen"))
print(msg.endswith("123"))
>>>True
True
lower 和 upper
msg = "shEn:20:mAle:123"
print(msg.lower())
print(msg.upper())
>>>shen:20:male:123
SHEN:20:MALE:123
string.replace()
replace(self, __old, __new, __count)
msg = "shen:shen:20:male:123"
print(msg.replace('shen','SHEN'))
print(msg.replace('shen','SHEN',1))
>>>SHEN:SHEN:20:male:123
SHEN:shen:20:male:123
string.join()
string.join()把列表拼接成字符串。
按照每个分隔符(引号内),把元素全为字符串的列表拼接成一个字符串
msg = ['shen','shen','20','male','123']
res = "".join(msg)
print(res)
>>>shenshen20male123
msg = ['shen','shen','20','male','123']
res = ":".join(msg)
print(res)
>>>shen:shen:20:male:123
a = 12345
print(a,type(a))
a = str(a)
print(a,type(a))
a = list(a)
print(a,type(a))
a = " ".join(a)
print(a,type(a))
>>>12345 <class 'int'>
12345 <class 'str'>
['1', '2', '3', '4', '5'] <class 'list'>
1 2 3 4 5 <class 'str'>
需要了解的内置操作(引用Egon老师的笔记)
#find,rfind,index,rindex,count
name='egon say hello'
print(name.find('o',1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
# print(name.index('e',2,4)) #同上,但是找不到会报错
print(name.count('e',1,3)) #顾头不顾尾,如果不指定范围则查找所有
#center,ljust,rjust,zfill
name='egon'
print(name.center(30,'-'))
print(name.ljust(30,'*'))
print(name.rjust(30,'*'))
print(name.zfill(50)) #用0填充
#expandtabs
name='egon\thello'
print(name)
print(name.expandtabs(1))
#captalize,swapcase,title
print(name.capitalize()) #首字母大写
print(name.swapcase()) #大小写翻转
msg='egon say hi'
print(msg.title()) #**每个单词的首字母大写**
#is数字系列
#在python3中
num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='四' #中文数字
num4='Ⅳ' #罗马数字
#isdigt:bytes,unicode
print(num1.isdigit()) #True
print(num2.isdigit()) #True
print(num3.isdigit()) #False
print(num4.isdigit()) #False
#isdecimal:uncicode
#bytes类型无isdecimal方法
print(num2.isdecimal()) #True
print(num3.isdecimal()) #False
print(num4.isdecimal()) #False
#isnumberic:unicode,中文数字,罗马数字
#bytes类型无isnumberic方法
print(num2.isnumeric()) #True
print(num3.isnumeric()) #True
print(num4.isnumeric()) #True
#三者不能判断浮点数
num5='4.3'
print(num5.isdigit())
print(num5.isdecimal())
print(num5.isnumeric())
'''
总结:
最常用的是isdigit,可以判断bytes和unicode类型,这也是最常见的数字应用场景
如果要判断中文数字或罗马数字,则需要用到isnumeric
'''
#is其他
print('===>')
name='egon123'
print(name.isalnum()) #字符串由字母或数字组成
print(name.isalpha()) #字符串只由字母组成
print(name.isidentifier())
print(name.islower())
print(name.isupper())
print(name.isspace())
print(name.istitle())