# 1.下标 # 列表与元组支持下标索引好理解,字符串实际上就是字符的数组,所以也支持下标索引。 # #如果想取出部分字符,那么可以通过下标的方法,(注意python中下标从 0 开始) name = 'abcdef' print(name[0]) #a print(name[1]) #b print(name[2]) #c # 2.切片 # 切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片操作。 # # 切片的语法:[起始:结束:步长] # 注意:选取的区间属于左闭右开型,即从"起始"位开始,到"结束"位的前一位结束(不包含结束位本身)。 # # 如果取出一部分,则可以在中括号[]中,使用: str = 'abcdef' print(str[0:3]) # 取 下标0~2 的字符 abc print(str[3:5]) # 取 下标为3、4 的字符 de print(str[2:]) # 取 下标为2开始到最后的字符 cdef print(str[:3]) # 取 下标为0开始到2的字符 abc print(str[::2]) # 取 间隔为2的 ace print(str[1:5:2]) # 取 一到5间隔2字符 bd print(str[::-2]) # 取 反向间隔2字符 fdb print(str[::-1]) # 反转字符串 fedcba mystr = 'hello world itcast and itcastcpp' #<1>find # 检测 str 是否包含在 mystr中,如果有返回开始的索引值,否则返回-1 # mystr.find(str, start=0, end=len(mystr)) print(mystr.find("itcast")) #12 print(mystr.find("itcast",0,10)) #-1 # <2>index # 跟find()方法一样,只不过如果str不在 mystr中会报一个异常(出错). # mystr.index(str, start=0, end=len(mystr)) # <3>count # 返回 str在start和end之间 在 mystr里面出现的次数 # mystr.count(str, start=0, end=len(mystr)) print(mystr.count("itcast")) # <4>replace # 把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次. # mystr.replace(str1, str2, mystr.count(str1)) mystr = "hello word ha ha" print(mystr.replace("ha","wa")) #hello word wa wa print(mystr.replace("ha","wa",1)) #hello word wa ha # <5>split # 以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串 mystr = "hello word ha ha" print(mystr.split(" ")) #以空格分割 ['hello', 'word', 'ha', 'ha'] print(mystr.split(" ",2))#['hello', 'word', 'ha ha'] #rsplit mystr = "auth.crsf.CORS" print(mystr.rsplit(".")) #以右边.分割 ['auth', 'crsf', 'CORS'] ret=mystr.rsplit(".",maxsplit=1) print(ret) #['auth.crsf', 'CORS'] print(ret[0],ret[1]) #ret[0]为auth.crsf # ret[1]为CORS str1,str2 = mystr.rsplit(".",maxsplit=1) #可以分别赋值 print(str1,str2) #str1为auth.crsf # str2为CORS # <6>capitalize # 把字符串的第一个字符大写 mystr = "hello word ha ha" print(mystr.capitalize()) #Hello word ha ha # <7>title # 把字符串的每个单词首字母大写 mystr = "hello word ha ha" print(mystr.title()) #Hello Word Ha Ha # <8>startswith # 检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False mystr = "hello word ha ha" print(mystr.startswith("hello")) #True # <9>endswith # 检查字符串是否以obj结束,如果是返回True,否则返回 False. print(mystr.endswith("hello")) #False # <10>lower # 转换 mystr 中所有大写字符为小写 mystr = "HELLO word ha ha" print(mystr.lower()) #hello word ha ha # <11>upper # 转换 mystr 中的小写字母为大写 print(mystr.upper()) #HELLO WORD HA HA # <12>ljust # 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串 # mystr.ljust(width) mystr = "hello" print(mystr.ljust(10)) #'hello ' # <14>center # 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串 mystr = "hello word ha ha" print(mystr.center(30)) #" hello word ha ha " # <15>lstrip # 删除 mystr 左边的空白字符 mystr = " hello" print(mystr.lstrip()) #hello # <16>rstrip # 删除 mystr 字符串末尾的空白字符 mystr = "hello " print(mystr.rstrip()) #hello # <17>strip
#同时去掉*&和空格
str = "***hello world &&&&"
print(str.strip('*& ')) #hello world
# 删除mystr字符串两端的空白字符 mystr = " hello " print(mystr.strip()) #hello # <18>rfind # 类似于 find()函数,不过是从右边开始查找. # mystr.rfind(str, start=0,end=len(mystr) ) mystr = "hello word ha ha" print(mystr.rfind("word")) #6 # <19>rindex # 类似于 index(),不过是从右边开始. # mystr.rindex( str, start=0,end=len(mystr)) mystr = "hello word ha ha" print(mystr.rindex("word")) #6 # <20>partition # 把mystr以str分割成三部分,str前,str和str后 # mystr.partition(str) mystr = "hello word ha ha" print(mystr.partition("word")) #('hello ', 'word', ' ha ha') # <21>rpartition # 类似于 partition()函数,不过是从右边开始. # mystr.rpartition(str) mystr = "hello word ha word ha" print(mystr.rpartition("word")) #('hello word ha ', 'word', ' ha') # <22>splitlines # 按照行分隔,返回一个包含各行作为元素的列表 # mystr.splitlines() mystr="hello\nword" print(mystr.splitlines()) #['hello', 'word'] # <23>isalpha # 如果 mystr 所有字符都是字母 则返回 True,否则返回 False mystr="hello" print(mystr.isalpha()) #True # <24>isdigit # 如果 mystr 只包含数字则返回 True 否则返回 False. mystr="hello" print(mystr.isdigit()) #False # <25>isalnum # 如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False mystr="hello" print(mystr.isalnum()) #True # <26>isspace # 如果 mystr 中只包含空格,则返回 True,否则返回 False. mystr="hello" print(mystr.isspace()) #False # <27>join # mystr 中每个字符后面插入str,构造出一个新的字符串 str = "-"; mystr = ("a", "b", "c") # 字符串序列 print(str.join(mystr)) #a-b-c import os # os.getcwd() #用以获取当前的工作目录 # m = os.path.join('路径','文件名.txt' path = os.path.join('app01/','views/','userviews') print(path) print(os.getcwd()) #D:\workplace\work\bbs_001