当前位置: 首页 > Linux

python_bomb----字符串操作

时间:2023-04-06 11:54:07 Linux

字符串创建由单引号、双引号和三引号括起来的字符组成formatescapecharacter反斜杠加单个字符可以表示一个特殊字符,通常是不可打印的字符/t='tab',/n='newline',/"='双引号本身'占位字符|%d|Integer||%f|浮点数||%s|String||%x|Start'h'>>>h[-1]#反向索引从-1'o'slices[start:end:step]#从start到end-1,步长为step;-如果start省略,则从头开始切片;-如果省略end,则切片到字符串末尾;s[1:]s[:-1]s[::-1]#反转字符串s[:]#ForStringcopymemberoperatorin|notin>>>'o'insTrue>>>'a'insFalse>>>'a'not在sTrue连接中a='hello'b='sheenstar'print("%s%s"%(a,b))hellosheenstara+b'hellosheenstar'a+''+b'hellosheenstar'repeatprint('*'*20+a+''+b+"*"*20)**********************你好sheenstar**********************字符串常用方法大小写'isalnum','isalpha','isdigit','islower','isspace','istitle','isupper''lower','upper','标题''你好'。tle()#判断是否为标题True'780abc'.isalnum()#判断是否为数字或字母True'780'.isdigit()#判断是否为数字True'abd'.isalpha()#判断是否为字母True'abd'.upper()#转为大写'ABD''ADE'.lower()#转为小写'ade''sheenSTAR'.swapcase()'SHEENstar'匹配endswithstartswithname="yum.repo"ifname.endswith('repo'):print(name)else:print("error")yum.repo去掉左右空格striplstriprstrip注意:去掉左右空格,空格是广义空间,包括:n,t,r>h='hello'>h.strip()'hello'>h'hello'>h.rstrip()'hello'>h'hello'>h.lstrip()'hello'搜索并替换find:searchreplace:replacecount:出现次数>>>h="hellosheen.hellopython">>>h.find("sheen")6>>>h.rfind("sheen")#从右边开始查找,输出仍然向前索引值6>>>h.rfind("python")19>>>h.replace("sheen",'star')'hellostar.hellopython'>>>h'hellosheen.hellopython'>>>h.count('hello')2>>>h.count('e')4分离拼接split:separationjoin:拼接>>>date="2018/08/11">>>date.split('/')['2018','08','11']>>>type(date.split('/'))>>>list=["1","3","5","7"]>>>"+".join(list)'1+3+5+7'