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

相关推荐
Tony Bai1 天前
高并发后端:坚守 Go,还是拥抱 Rust?
开发语言·后端·golang·rust
wjs20241 天前
Swift 类型转换
开发语言
秃了也弱了。1 天前
python实现定时任务:schedule库、APScheduler库
开发语言·python
weixin_440730501 天前
java数组整理笔记
java·开发语言·笔记
Thera7771 天前
状态机(State Machine)详解:原理、优缺点与 C++ 实战示例
开发语言·c++
niucloud-admin1 天前
java服务端——controller控制器
java·开发语言
夏幻灵1 天前
JAVA基础:基本数据类型和引用数据类型
java·开发语言
cike_y1 天前
Spring-Bean的作用域&Bean的自动装配
java·开发语言·数据库·spring
十八度的天空1 天前
第01节 Python的基础语法
开发语言·python
我是唐青枫1 天前
深入理解 C#.NET Interlocked.Increment:原子操作的核心
c#·.net