连接join操作:split是把一个字符串分割成很多字符串组成的list,而join则是把一个list中的所有字符串连接成一个字符串 用法:newstring='连接符号'.join(列表)
s1='www.blog.51cto.com/blogger/draft/782804' #count检索指定字符串在另一个字符串中出现次数.如果检索的字符串不存在,则返回0 #用法str.count(self,x,__start,__end)可以指定范围 print(s1.count('w')) #统计’w'出现的次数 #index用法,返回首次出现的位置 print(s1.index('5')) #若不存在“ValueError: substring not found” #str.find(sub[, start[, end]]) 检索的字符串不存在,返回-1 , # 否则返回首次出现该子字符串时的索引 print(s1.find('5')) #endswith检索字符串是否以子字符串结尾,如果是就返回True,否则返回False print(s1.endswith('04')) strip操作: s1=' www.blog.51cto.com/blogger/draft/782804' s2='@hello python@.' print(s1) #str.strip(self,chars)去除首位空格或特殊字符 print(s1.strip()) print(s2.strip('@'+'.')) #+表示或的关系,首尾包含任一指定字符都将删除 #去除左边特殊字符str.lstrip(self,chars) print(s2.lstrip('@')) #去除右边特殊字符.rstrip() print(s2.rstrip('.'))
本文分享自 作者个人站点/博客 前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体分享计划 ,欢迎热爱写作的你一起参与!