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

相关推荐
globaldomain1 小时前
什么是用于长距离高速传输的TCP窗口扩展?
开发语言·网络·php
沈阳信息学奥赛培训1 小时前
#undef 指令 (C/C++)
c语言·开发语言·c++
2401_873204652 小时前
分布式系统安全通信
开发语言·c++·算法
Dxy12393102162 小时前
JS发送请求的方法详解
开发语言·javascript·ecmascript
sw1213893 小时前
C++中的代理模式实战
开发语言·c++·算法
難釋懷3 小时前
Lua语法入门-条件控制、函数
开发语言·junit·lua
桌面运维家3 小时前
Win10打印机共享故障排查:权限与网络配置详解
开发语言·网络·php
Sunshine for you4 小时前
实时操作系统中的C++
开发语言·c++·算法
史蒂芬_丁4 小时前
C++深度拷贝例子
java·开发语言·c++
Knight_AL4 小时前
Nacos 启动问题 Failed to create database ’D:\nacos\nacos\data\derby-data’
开发语言·数据库·python