Python学习⽇记(⼀)String函数使⽤
s = "abcaDa a"
s2 = "123a abc ABCSAa s "
s3 = "\tas \t\tb123"
s4 = ' &abc123 c ## '
1.str.capitalize()
将原字符串内的⾸字母转成⼤写,其他部分⼩写,再返回新字符串
print("s.capitalize() = {function}".format(function = s.capitalize()))
Output:
s.capitalize() = Abcada a
2.str.upper()
将原字符串的字母转为⼤写
print("s.upper() = {function}".format(function = s.upper()))
Output:
s.upper() = ABCADA A
3.str.lower()
将原字符串的字母转为⼩写
print("s.lower() = {function}".format(function = s.lower()))
Output:
s.lower() = abcada a
4.str.swapcase()
将原字符串内的⼤写⼩写反转
print("s.swapcase() = {function}".format(function = s.swapcase()))
Output:
s.swapcase() = ABCAdA A
5.str.title()
原字符串内如果有特殊字符(包括数字)连接字母,则将特殊字符后的⾸个英⽂字母转化为⼤写形态,并返回新字符串
print("s2.title() = {function}".format(function = s2.title()))
Output:
s2.title() = 123A Abc Abcsaa S
()
<(宽度,填充字符) 将字符串以居中的格式返回,若宽度值⽐len(s)⼩则返回原字符串,填充以从左到右为规则,填充字符的默认值为空格,值可以⾃⼰更改
print("s2.center() = {function}".format(function = s2.center(19,'&')))
print("s2.center() = {function}".format(function = s2.center(20,'&')))
Output:
#s2 = 123a abc ABCSAa s
<() = &123a abc ABCSAa s
<() = &123a abc ABCSAa s &
pandtabs()
往上+1,就相当于增加⼀个空格
print("s3.expandtabs ={function}".format(function = s3.expandtabs()))
print("s3.expandtabs ={function}".format(function = s3.expandtabs(0)))
print("s3.expandtabs ={function}".format(function = s3.expandtabs(5)))
print("s3.expandtabs ={function}".format(function = s3.expandtabs(8)))
print("s3.expandtabs ={function}".format(function = s3.expandtabs(9)))
Output:
#s3 = "\tas \t\tb123"
8.String_len = len(str)
公共⽅法,计算字符串的长度
print("s2_length = {0}".format(len(s2)))
Output:
s2_length = 18
9.str.startswith()
str.startswith(substr,strbeg,strend) 判断原字符串是否以⼦字符串开头可以⽤切⽚的形式进⾏判断(以顾头不顾尾为原则),这⾥的strend要⽐strbeg⼤否则传回false 函数结果返回⼀个布尔值
print("s.startswith() = {function}".format(function = s.startswith('ab')))
print("s.startswith() = {function}".format(function = s.startswith('D',2)))
print("s.startswith() = {function}".format(function = s.startswith('c',2,5)))
print("s.startswith() = {function}".format(function = s.startswith('c',2,100)))
Output:
#s = abcaDa a
s.startswith() = True
s.startswith() = False
s.startswith() = True
s.startswith() = True
dswith()
print("s.endswith() = {function}".format(function = s.endswith('Da a')))
print("s.endswith() = {function}".format(function = s.endswith('a',-1)))
print("s.endswith() = {function}".format(function = s.endswith('Da a',-4)))
print("s.endswith() = {function}".format(function = s.endswith('c',-8,-5)))
print("s.endswith() = {function}".format(function = s.endswith('c',-7,-5)))
print("s.endswith() = {function}".format(function = s.endswith('c',-7,-4)))
print("s.endswith() = {function}".format(function = s.endswith('Da a',-1,6)))
Output:
#s = abcaDa a
字符串和函数是什么11.str.find()
str.find(substr,strbeg,strend) 寻字符串中是否存在该⼦字符串并返回其索引,不到则返回-1 若同时出现相同的字符串则返回最先到
的⼦字符串索引,切⽚(遵循顾头不顾尾原则)
print("s.find() = {function}".format(function = s.find('a')))
print("s.find() = {function}".format(function = s.find('ab')))
print("s.find() = {function}".format(function = s.find('f')))
print("s.find() = {function}".format(function = s.find('b',-8,-5)))
print("s.find() = {function}".format(function = s.find('b',0)))
Output:
s.find() = 0
s.find() = 0
s.find() = -1
s.find() = 1
s.find() = 1
12.str.index()
str.index(substr,strbeg,strend) 与find()的功能⼤致相同,但⼦字符串不存在于字符串中就会报错,结果返回索引
print("s.index() = {function}".format(function = s.index('a')))
print("s.index() = {function}".format(function = s.index('ab')))
print("s.index() = {function}".format(function = s.index('D',-8,5)))
print("s.index() = {function}".format(function = s.index('b',0)))
Output:
#s = abcaDa a
s.index() = 0
s.index() = 0
s.index() = 4
s.index() = 1
13.str.strip()
str.strip([chars]) 去掉字符串中⾸和尾的字符默认删除的是空格 chars可以⾃⾏更改
print("s3.strip() = {function}".format(function = s3.strip()))
Output:
#s3 = as b123
s3.strip() = as b123
14.str.rstrip()
str.rstrip([chars]) 去掉字符串右边的字符默认删除的是空格返回值为删除后的新字符串
print("s4.rstrip() = {function}".format(function = s4.rstrip()))
print("s4.rstrip() = {function}".format(function = s4.rstrip('# c')))
Output:
#s4 = ' &abc123 c ## '
s4.rstrip() = &abc123 c ##
s4.rstrip() = &abc123
15.str.lstrip()
str.lstrip([chars]) 去掉字符串左边的字符默认删除的是空格返回值为删除后的新字符串
print("s4.lstrip() = {function}".format(function = s4.lstrip()))
print("s4.lstrip() = {function}".format(function = s4.lstrip(' &')))
Output:
#s4 = ' &abc123 c ## '
s4.lstrip() = &abc123 c ##
s4.lstrip() = abc123 c ##
unt()
的个数并返回个数
print("s.count() = {function}".format(function = s.count('a')))
print("s.count() = {function}".format(function = s.count('Fa')))
print("s.count() = {function}".format(function = s.count('a',-7)))
Output:
#s = "abcaDa a"
17.str.split()
str.split(substr = " ",num = s.count(str)) 主要作⽤分割字符串这⾥的substr默认以空格分割,num为总共切割的次数,若num有数值怎分割num+1个字符串分割后将字符串转化为list形式 substr会在分割后消失
print("s2.split() = {function}".format(function = s2.split()))
print("s2.split() = {function}".format(function = s2.split('',0)))
print("s2.split() = {function}".format(function = s2.split('a',1)))
print("s2.split() = {function}".format(function = s2.split('a',-1)))
Output:
#s2 = "123a abc ABCSAa s "
s2.split() = ['123a', 'abc', 'ABCSAa', 's']
s2.split() = ['123a abc ABCSAa s ']
s2.split() = ['123', ' abc ABCSAa s ']
s2.split() = ['123', '', 'bc ABCSA', ' s ']
18.str.format()
三种⽤法:
print("s = {}|s2 = {}|s3 = {}".format(s,s2,s3))
print("s = {0}|s2 = {1}|s3 = {2}|s = {0}|s3 = {2}".format(s,s2,s3))
print("s = {s}{sep}s2 = {s2}{sep}s3 = {s3}".format(s = s,s2 = s2,s3 = s3,sep = '|'))
Output:
s = abcaDa a|s2 = 123a abc ABCSAa s |s3 = as b123
s = abcaDa a|s2 = 123a abc ABCSAa s |s3 = as b123|s = abcaDa a|s3 = as b123
s = abcaDa a|s2 = 123a abc ABCSAa s |s3 = as b123
当占位符不按顺序写会报错tuple index out of range
place()
字符串
print("s.replace() = {function}".format(function = s.replace('ac','A')))
print("s.replace() = {function}".format(function = s.replace('a','A')))
print("s.replace() = {function}".format(function = s.replace('a','A',2)))
Output:
#s = "abcaDa a"
20.is函数
str.isalnum() #判断是否由数字或字母组成
str.isalpha() #判断是否含有字母
str.isdecimal() #判断是否含有⼗进制数字
str.isdigit() #判断是否含有数字
str.islower() #判断是否含有⼩写字母
str.isupper() #判断是否含有⼤写字母
str.isnumeric() #判断是否只包含数字字符
str.isspace() #判断是否只含有空格
str.istitle() #判断是否经过title()函数处理过后的标题
21.join()
str.join(sequence) sequence为要连接的元素序列,函数作⽤是将指定字符串与原字符串中每⼀个字符⼀⼀连接s = 'alex'
print('&&'.join(s)) #a&&l&&e&&x
l = ['a','l','e','x']
print('+'.join(l)) #a+l+e+x
t = ('a','b')
print('*'.join(t)) #a*b
PS:String索引对应
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论