python 字符处理

判断

result = url.isalpha()  #判断是否是字母
result = url.isdigit()  #判断是否是数字
result = filename.endswith("doc")  #判断结束字符
result = filename.startswith("a")  #判断开始字符

查找

string = "sdfsdfsdf asdf"
#find 查找到返回索引,找不到返回-1
result = string.find("/")  #str.find(sub, start=0, end=-1)
result = string.lfind("/") 
result = string.rfind() 
#index 查找到返回索引,找不到返回异常
result = string.index()  ##str.index(sub, start=0, end=-1)
result = string.rindex() 
result = string.lindex()

替换

#replace() strip() lstrip()  rstrip()
#upper() lower() capitalize() title()
#str.replace(old, new, count) -> str
url = url.replace('/','#')
aaa = "   sdf asdas ".strip() #去除空格
aaa = "   sdf asdas ".lstrip()
aaa = "   sdf asdas ".rstrip()
aaa.strip().upper() #变为大写
aaa.strip().lower() #变为小写
aaa.strip().title() #单词首字母大写
aaa.strip().capitalize()  #第一个字母大写

分割

#split()
#str.split(sep, maxsplit) -> list
list = url.split("#")

#list[start:end:step] 默认step = 1
#list标号有两套[0,len(list)-1] [-1,-len(list)]
list = [x for x in range(10)]
list1 = list[::-1]
list2 = list[-1:-len(list)-1:-1]

拼接

# join()
# str.join(iterable)
aaa = "-".join(list)

posted @ 2020-04-15 18:01  tomyyyyy  阅读(170)  评论(0编辑  收藏  举报