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))

相关推荐
yueyuebaobaoxinx2 小时前
MATLAB 与 Simulink 联合仿真:控制系统建模与动态性能优化
开发语言·matlab·性能优化
superlls3 小时前
(计算机网络)JWT三部分及 Signature 作用
java·开发语言·计算机网络
★YUI★3 小时前
学习制作记录(选项UI以及存档系统)8.24
学习·游戏·ui·unity·c#
一只鲲3 小时前
56 C++ 现代C++编程艺术5-万能引用
开发语言·c++
liulilittle4 小时前
.NET反射与IL反编译核心技术
开发语言·数据库·c#·.net·反射·反编译·il
月巴月巴白勺合鸟月半4 小时前
Teams Bot机器人实时语音识别的多引擎的处理
机器人·c#
扛麻袋的少年4 小时前
6.Kotlin的Duration类
android·开发语言·kotlin
Tony Bai6 小时前
泛型重塑 Go 错误检查:errors.As 的下一站 AsA?
开发语言·后端·golang
hixiong1236 小时前
C# 编写一个XmlToDota的转换工具
开发语言·人工智能·yolo·c#
hqxstudying6 小时前
JAVA限流方法
java·开发语言·安全·限流