#PS:本文包含了day10中字符串部分的知识
字符串的魔法
【capitalize()首字母大写】
test = ‘alex’
v = test.capitalize()
print(v) #Alex
【lower():变小写(只能处理普通的对应关系,比如英文字符的转换)】
test = ‘aLex’
v2 = test.lower()
print(v2) #alex
【casefold():变小写(可以处理更复杂的对应关系,比如法语大小写)】
test = ‘aLex’
v1 = test.casefold()
print(v1)
【swapcase()大小写转换】
test = ‘alex’
v = test.swapcase()
print(v) #ALEX
【center()设置宽度并将内容居中】
def center(self , width , fillchar = None) #width代指总长度,fillchar是填充字符(可有可无)
PS:fillchar = None这种有等号的可以写可以不写,但是没有等号的项一定要写
举例1:
test = ‘aLex’
v = test.center(20)
print(v)
举例2:
test = ‘aLex’
v = test.center(20,’*’)
print(v)
【ljust()和rjust()】
举例1:
test = ‘alex’
v = test.ljust(10, ‘*’)
print(v) #alex******
举例2:
test = 'alex'
v = test.rjust(10,'*')
print(v)
输出为:******alex
【zfill()填充0】
test = ‘alex’
v = test.zfill(10)
print(v) #000000alex
【count()去字符串中寻找子序列出现的次数】
def count(self , sub , start = None , end = None)
举例1:
test = ‘aLexalex’
v = test.count(‘r’)
print(v) #0
举例2:
test = ‘aLexalex’
v = test.count(‘ex’ , 5 , 6) #在5~6范围内找ex子序列出现的次数
print(v) #0
test = ‘aLexalex’
v = test.count(‘ex’ , 5 , 7) #在5~7范围内找ex子序列出现的次数
print(v) #0
test = ‘aLexalex’
v = test.count(‘ex’ , 5 , 8) #在5~8范围内找ex子序列出现的次数
print(v) #1
【uncode() 将其他编码的字符串解码(decode)成unicode】
【decode() 将unicode编码(encode)成另一种编码】
【endswith()是否以。。。结尾】
test = ‘alex’
v = test.endswith(‘ex’)
print(v) #True
【startswith()是否以。。。开始】
v = 'abc'
i = v.startswith('a')
print(i) #True
【find()从开头往后找,找到第一个之后,获取其位置】
def find(self , sub , start = None , end = None)
举例1:
test = ‘alexalex’
v = test.find(‘ex’)
print(v) #2
举例2:
test = 'alexalex'
v = test.find('ex', 5)
print(v) #6
举例3:
test = ‘alexalex’
v = test.find(‘ex’ , 5 , 8)
print(v) #6
注意:[5,8)是左闭右开区间,一般都是大于等于5,小于8
【format()格式化,将一个字符串中的占位符替换为指定的值】
def format(*args,**kwargs):
# **kwarges代表用name=‘alex’这种方式也可以传字典
举例1:
test = ‘i am {name}’
print(test) #i am {name}
v = test.format(name = ‘alex’)
print(v) #i am alex
举例2:
test = ‘i am {name} , age {a}’
print(test) #i am {name} , age {a}
v = test.format(name = ‘alex’ , a = 19)
print(v) #i am alex , age 19
--------------也可以这么写--------------
test = 'i am {name},age {a}'
v = test.format(**{'name':'alex','a':19})
print(v) #i am alex,age 19
举例3:
test = 'i am {0} , age {1}' #大括号里面要用数字???
print(test)
v = test.format('alex',19)
print(v)
#i am {0} , age {1}
#i am alex , age 19
举例4:
template = 'name:{0},age:{1}'
name = input('>>>')
v = template.format(name,18)
print(v) #name:carrie,age:18
【format_map()格式化,传入的值是一对一对出现的】
举例1:
test = ‘i am {name} , age {a}’
v1 = test.format(name = ‘df’ , a = 10)
v2 = test.format_map ({‘name’: ‘alex’ , ‘a’ : 19})
print(v1)
print(v2)
【index()】
举例1:
test = ‘alexalex’
v = test.index(‘ex’)
print(v) #2
举例2:
test = ‘alexalex’
v = test.index(‘8’)
print(v) #index()和find()的区别是:index()找不到的时候报错,而find()找不到的时候返回-1
【isalnum()字符串中是否只包含字母和数字】
例子:
test = ‘uasf890’
v = test.isalnum()
print(v) #true
【expandtabs()断句】
解释:
expandtabs() 方法把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8。
从头开始数,数到第一个\t正好为8个空格,不足则补空格,如果还有\t,接着从第一个\t数到第二个\t仍然为8个空格,以此类推直到最后一个\t结束。
相关资料网址:
举例1:
s = ‘12345678\t9’
v = s.expandtabs(6)
print(v , len(v)) #12345678 9 13(中间有四个空格)
举例2:
test=‘username\temail\tpassword\nlaiying\tying@qq.com\t123\nlaiying\tying@qq.com\t123\nlaiying\tying@qq.com\t123’
v = test.expandtabs(20)
print(v)
#输出结果:
#username email password
#laiying ying@qq.com 123
#laiying ying@qq.com 123
PS:\t是水平制表符;\n是换行符
【isalpha()判断是否全是字母或汉字】
test = ‘abc’
v = test.isalpha()
print(v) #true
【isdigital()判断一个字符串内是否全为数字】
举例1:
test = ‘123’
v1 = test.isdecimal()
v2 = test.isdigit()
print(v1 ,v2) #True True
举例2:
test = ‘②’
v1 = test.isdecimal()
v2 = test.isdigit()
print(v1 ,v2) #False True
注意:对于②之类的字符isdecimal的判断结果为false,isdigit的判断结果为true
【isidentifier()只要出现字母,数字,下划线,就认为是标识符(数字不能开头)】
example1:
a =’_123’
v = a.isidentifier()
print(v) #True
example2:
a =’123’
v = a.isidentifier()
print(v) #False
【.islower()判断字符串中是否全为小写】
【.lower()转换为小写】
test = ‘Alex’
v1 = test.islower()
v2 = test.lower()
print(v1,v2) #False alex
【.isupper()】
【.upper()】
【strip()系列】
example1:
test = ‘ alex ’
v1 = test = lstrip()
v2 = test = rstrip()
v3 = test = strip()
print(v1,v2,v3)
example2:
test = ‘\nalex’
v = test = lstrip()
print(v) #lstrip既可以移除空白,也可以移除换行,去除\n和\t和指定字符
example3:
test = ‘xalex’
v = test.lstrip(‘x’) #可以指定去除的内容
print(v) #alex
example4:
test = ‘xalex’
v = test.rstrip(‘9aexa’) #只要9aexa中有子序列能和xalex匹配,就会从xalex中去除匹配的部分,匹配顺序是:先匹配最长的子序列字符串,rstrip从右往左匹配,一旦没有可以匹配的子序列,就终止匹配
print(v) #xal
【maketrans()和translate()】
v = 'sakjhahfolfkna'
m = str.maketrans('aeiou', '12345')
new_v = v.translate(m)
print(new_v) #s1kjh1hf4lfkn1
【isnumeric()判断是否为数字】
举例1:
test = ‘②’
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1 , v2 , v3) #False True True
举例2:
test = ‘二’
v1 = test.isdecimal() #isdecimal用的比较多
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1 , v2 , v3) #False False True
【isprintable()字符串中是否包含不可显示的字符(比如\n或者\t)】
举例1:
test = ‘salsaljaslifh’
v = test.isprintable()
print(v) #True
举例2:
test = ‘salsal\tjaslifh’
v = test.isprintable()
print(v) #False
【isspace()判断是否全部都是空格】
举例1:
test = ‘gakbifus’
v = test.isspace()
print(v) #False
举例2:
test = ‘’
v = test.isspace()
print(v) #False
【istitle()判断这个字符串是不是符合标题的样式(首字母大写)】
test = ‘hfanfka’
v = test.istitle()
print(v)
【title()将字符串转换成标题的样式】
test = 'i am a boy'
v = test.title()
print(v)
#I Am A Boy
【join()将字符串中的每一个元素按照指定分隔符进行拼接】
example1:
test = ‘你是风儿我是沙’
print(test)
t = ‘ ’
v = t.join(test)
print(v) #你 是 风 儿 我 是 沙
example2:
test = ‘你是风儿我是沙’
print(test)
v = ‘ ’.join(test)
print(v) #你 是 风 儿 我 是 沙
example3:
test = ‘你是风儿我是沙’
print(test)
v = ‘_’.join(test)
print(v) #你_是_风_儿_我_是_沙
example4:
v = '_'.join(['alex','eric'])
print(v) #alex_eric
【partition()分割后能拿到分割的元素】
test = ‘testforsme’
v = test.partition(‘s’)
print(v) #(‘te’ , ‘s’ , ‘tforsme’)
【split()分割后不能拿到分割的元素】
example1:
test = 'testforsme'
v1 = test.split('s')
v2 = test.split('s', 2)
print(v1 , v2) #['te', 'tfor', 'me'] ['te', 'tfor', 'me']
example2:
test = ‘testforsme’
v = test.split(‘s’)
print(v) #[‘te’ , ‘tfor’ , ‘me’](其实这就是列表)
example3:
value = '5+9'
v1,v2 = value.split('+')
sum = int(v1)+int(v2)
print(sum) #14
【rsplit()】
test.rsplit()
PS:partition分成三个元素,split可以自主选择分成多少个
应用:
‘1*2/7*99+8+5’
(正则表达式中,可以指定是否想要分割的元素)
【列表】
li = [11 , 22 , 33 , 44 , 55 , ‘asdf’]
len(‘sjfauofhlanf’) #计算字符串的长度
len(li) #计算列表中有几个元素
【splitlines()】
说明:
Python splitlines() 按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
语法:
str.splitlines([keepends])
相关资料链接:
example1:
test = ‘slsafljl\nasdasjoduhn\nusiad’
v = test.splitlines(True) #['slsafljl\n', 'asdasjoduhn\n', 'usiad']
print(v)
example2:
test = ‘slsafljl\nasdasjoduhn\nusiad’
v = test.splitlines(False) #['slsafljl', 'asdasjoduhn', 'usiad']
print(v)
【最长公共子序列】
‘alex’
‘uilex’
#上面两字符串的最长公共子序列为lex
【replace()】
example1:
test = ‘alexalexalex’
v = test.replace(‘ex’ , ‘bbb’)
print(v) #将所有的ex都替换成bbb
example2:
test = ‘alexalexalex’
v = test.replace(‘ex’ , ‘bbb’ , 1)
print(v) #只将第一个ex替换成bbb
example3:
test = ‘alexalexalex’
v = test.replace(‘ex’ , ‘bbb’ , 2)
print(v) #将前两个ex替换成bbb
##################下面七个比较重要##################
join()
split()
find()
strip()
upper()
lower()
replace()
【[]索引,下标(从0开始),获取字符串中的某一个字符】
example1:
test =’alex’
v = test[3]
print(v) #x
example2:
test =’alex’
v = test[0:1] #大于等于0,小于1
print(v) #a
example3:
test =’alex’
v = test[0:2] #大于等于0,小于2
print(v) #al
example4:
test =’alex’
v = test[0:-1] #0=< 到最后这个位置(不包括最后一位)
print(v) #ale
【len()获取当前字符串中字符的个数】
example1:
test =’alex’
v = len(test)
print(v) #4
example2:
test =’郑建文’
v = len(test)
print(v) #3(但是在python2中得到9)
应用:一个一个字输出
test = ‘我是一句话’
index = 0
while index < len(test):
v = test[index]
print(v)
index += 1
print(‘=========’)
【for循环】
for 变量名 in 字符串:
.....
PS:只要能被for循环,就是可迭代对象
例子
test = ‘我是一句话’
for i in test:
print(i) #可以达到相同的效果
==========1个深灰魔法==========
字符串一旦被创建就不能修改,一旦修改或拼接,都会造成重新生成字符串
【range()帮助创建连续的数字】
example1:
v = range(100)
print(v) #range(0, 100) 此时还没创建
for i in v: #这时才创建,每循环一次创建一次
print(i) #输出的数字是0到99
注意:
1、python2中的range是立即创建,python3中的range是for循环的时候才一个一个创建;但是python2里面还有一个xrange,和python3中的range效果一样的,也就是在for循环的时候才一个个创建元素
2、v = range(100)相当于v = range(0 , 100) #大于等于0,小于100
example2:
v = range(0,100,5) #5是步长
for i in v: #这时才创建,每循环一次创建一次
print(i) #输出的数字是0,5,10,......,90,95
v = range(100,0,-1)
for i in v:
print(i) #100 99 98 .... 2 1
example3:将文字对应的索引打印出来
test = input(‘>>>’)
for item in range(0,len(test)):
print(item , test[item])
#若输入abcdef,则返回如下形式:
#0 a
#1 b
#2 c
#3 d
#4 e
#5 f
【类、对象、方法】
类:int、str、bool等等
方法:day11讲的那些都是针对不同类的方法
对象:根据某一类创建对象,比如:
s1 = ‘alex’
s2 = ‘alex’
这里的s1、s2就是根据str类型创建的两个对象