Python学习——字符串操作方法

mystr = "hello word goodbye"

str = "bye"

Find函数:检测一个字符串中是否包含另一个字符串,找到了返回索引值,找不到了返回-1

print(mystr.find(str,0,len(mystr)))

print(mystr.find(str,0,13))

index函数:检测一个字符串是否包含另一个字符串,找到了范围索引值,找不到了报错

print(mystr.index(str,0,len(mystr)))

print(mystr.index(str,0,12))

count函数:返回字符串在开始和结束之间另一个字符串出现的次数

print(mystr.count(str,0,len(mystr)))

replace函数:把字符串中的字符按照要求次数替换成新的字符

print(mystr.replace(str,"GOOD",1))

查看字符串的方法

print(dir(str))

自学方法

print(help(str.count))

print(help(str.find))

print(help(str.replace))

print(help(str.index))

相关推荐
TELL5218 分钟前
selenium webdriver 第二次初始化的异常
开发语言·python
动词ing36 分钟前
【C语言】自定义函数+指针入门
c语言·开发语言·算法
重生之后端学习1 小时前
283. 移动零[简单]✅
开发语言·数据结构·算法·leetcode·职场和发展
动词ing2 小时前
【C语言】结构体+文件基础
c语言·开发语言·数据结构
淡海水2 小时前
02-06-历史-托管GC机制-垃圾回收如何影响数据结构选择
c#·编译·gc·机器码
caimouse2 小时前
ReactOS 窗口系统分析(7):分层窗口与绘制辅助 — layered.c + draw.c
c语言·开发语言
丰锋ff2 小时前
基于 Qt 的智慧社区物业管理系统
开发语言·qt
夜不会漫长3 小时前
C++入门(1)
开发语言·c++·算法
大鹏说大话3 小时前
从爬虫到决策引擎:大数据下自媒体如何用Python挖掘用户痛点
开发语言·爬虫·python
Niuguangshuo3 小时前
silero-vad:超轻量级开源 VAD 实践指南
开发语言·python