https://blog.csdn.net/weixin_43158056/article/details/92798114
py_str = 'hello world!' py_str.capitalize() py_str.title() py_str.center(50) py_str.center(50, '#') py_str.ljust(50, '*') py_str.rjust(50, '*') py_str.count('l') # 统计l出现的次数 py_str.count('lo') py_str.endswith('!') # 以!结尾吗? py_str.endswith('d!') py_str.startswith('a') # 以a开头吗? py_str.islower() # 字母都是小写的?其他字符不考虑 py_str.isupper() # 字母都是大写的?其他字符不考虑 'Hao123'.isdigit() # 所有字符都是数字吗? 'Hao123'.isalnum() # 所有字符都是字母数字? ' hello\t '.strip() # 去除两端空白字符,常用 ' hello\t '.lstrip() ' hello\t '.rstrip() 'how are you?'.split() 'hello.tar.gz'.split('.') '.'.join(['hello', 'tar', 'gz']) '-'.join(['hello', 'tar', 'gz'])
# -*- coding: cp936 -*- 母串='cadefgh' 子串='f' s3 = 母串.find(子串)#结果为4 s4 = 母串.index(子串)#结果从0开始 #find方法和index方法都是用来查找目标字符串的索引位置,当目标字符串不存在,find查询返回-1,index则抛出异常。” print(母串,子串, s3 , s4) #求前子串 s='0123456n' s0=s[0:2]#前两个字符,其中0可省略 s1=s[:2] # 01 print(s0,s1) str = '0123456789′
print str[0:3] #截取第一位到第三位的字符
print str[:] #截取字符串的全部字符
print str[6:] #截取第七个字符到结尾
print str[:-3] #截取从头开始到倒数第三个字符之前
print str[2] #截取第三个字符
print str[-1] #截取倒数第一个字符
print str[::-1] #创造一个与原字符串顺序相反的字符串
print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符
print str[-3:] #截取倒数第三位到结尾
print str[:-5:-3] #逆序截取
将”to be or not to be”字符串倒序输出
>>> 'to be or not to be'[::-1]
将”sxtsxtsxtsxtsxt”字符串中所有的 s 输出
>>> 'sxtsxtsxtsxtsxt'[::3]
#求尾子串 s='0123456n' s2=s[3:] # 3456n print(s2) s.upper() #s大写
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律