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

相关推荐
Wang's Blog4 小时前
Go-Zero 项目开发43:基于 Filebeat 收集各个服务的日志信息
开发语言·golang·go-zero·filebeat
丈剑走天涯5 小时前
JDK 17 正式特性
java·开发语言
秋田君5 小时前
QT_QFontDialog类字体对话框
开发语言·qt
圣光SG6 小时前
Java操作题练习(七)
java·开发语言·算法
麻瓜老宋7 小时前
AI开发C语言应用按步走,表达式计算器calc的第二十三步,多行输入、进制输出、错误恢复、常量折叠、配置加载等
c语言·开发语言·atomcode
q567315237 小时前
企业级 HTTP 代理采购选型:技术评估清单 15 项
开发语言·网络·爬虫·网络协议·http·隧道ip·代理ip
天天进步20158 小时前
Python全栈项目--智能办公自动化系统
开发语言·python
czhc11400756638 小时前
7.29:树形;
c#
cm04Z9c918 小时前
C#摸鱼实录——IoC与DI案例详解
开发语言·c#
long3169 小时前
Java 新手入门与实战开发指南
java·开发语言