Tutorialsteacher

关注我们

文章
  • C#
  • C# 面向对象编程
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • 控制反转 (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go 语言
  • HTTPS (SSL)
  • 正则表达式
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Python - 入门
  • 什么是 Python?
  • Python 在哪里使用?
  • Python 版本历史
  • 安装 Python
  • Python - Shell/REPL
  • Python IDLE
  • Python 编辑器
  • Python 语法
  • Python 关键字
  • Python 变量
  • Python 数据类型
  • 数字
  • 字符串
  • 列表
  • 元组
  • 集合
  • 字典
  • Python 运算符
  • Python 条件 - if, elif
  • Python While 循环
  • Python For 循环
  • 用户定义函数
  • Lambda 函数
  • 变量作用域
  • Python 模块
  • 模块属性
  • Python 包
  • Python PIP
  • __main__, __name__
  • Python 内置模块
  • OS 模块
  • Sys 模块
  • Math 模块
  • Statistics 模块
  • Collections 模块
  • Random 模块
  • Python 生成器函数
  • Python 列表推导式
  • Python 递归
  • Python 内置错误类型
  • Python 异常处理
  • Python Assert 语句
  • 在 Python 中定义类
  • Python 中的继承
  • Python 访问修饰符
  • Python 装饰器
  • @property 装饰器
  • @classmethod 装饰器
  • @staticmethod 装饰器
  • Python 双下划线方法
  • Python 中的 CRUD 操作
  • Python 读写文件
  • Python 中的正则表达式
  • 使用 Tkinter 创建 GUI
Entity Framework Extensions - 提升 EF Core 9
  批量插入
  批量删除
  批量更新
  批量合并

Python 字符串的 index() 方法

index() 方法返回子字符串在给定字符串中第一次出现的索引。它与 find() 方法相同,不同之处在于如果找不到子字符串,它会引发异常。

语法

str.index(substr, start, end)

参数

  1. substr: (必需) 要查找索引的子字符串。
  2. start: (可选) 字符串中开始搜索的起始索引位置。默认为 0。
  3. end: (可选) 字符串中搜索结束的索引位置。默认为字符串的末尾。

返回值

一个整数值,表示指定子字符串的索引。

以下示例演示了 index() 方法。

示例: index()
greet='Hello World!' print('Index of H: ', greet.index('H')) print('Index of e: ', greet.index('e')) print('Index of l: ', greet.index('l')) print('Index of World: ', greet.index('World'))
输出
Index of H: 0 Index of e: 1 Index of l: 2 Index of World: 6

index() 方法只返回第一次出现的索引。

示例: index() 返回第一次出现的索引
greet='Hello World' print('Index of l: ', greet.index('l')) print('Index of o: ', greet.index('o'))
输出
Index of l: 2 Index of o: 4

index() 方法执行区分大小写的搜索。如果找不到子字符串,它会抛出 ValueError。

示例: 区分大小写的 index() 方法
greet='Hello World' print(greet.index('h')) # throws error: substring not found print(greet.index('hello')) # throws error
输出
ValueError: substring not found ValueError: substring not found

如果找不到给定的子字符串,它将抛出错误。

示例: index()
mystr='TutorialsTeacher is a free online learning website' print(mystr.index('python'))
输出
Traceback (most recent call last) <ipython-input-3-a72aac0824ca> in <module> 1 str='TutorialsTeacher is a free online learning website' ----> 2 print(str.index('python')) ValueError: substring not found

使用 start 和 end 参数将子字符串的搜索限制在指定的起始和结束索引之间。

示例: 带 start 和 end 参数的 index()
mystr='tutorialsteacher is a free tutorials website' print(mystr.index('tutorials',10)) # search starts from 10th index print(mystr.index('tutorials',1, 26)) # throws error; searches between 1st and 26th index
输出
27 ValueError: substring not found
TUTORIALSTEACHER.COM

TutorialsTeacher.com 是您权威的技术教程来源,旨在通过循序渐进的方法,指导您掌握各种网络和其他技术。

我们的内容旨在帮助所有水平的学习者轻松快速地学习技术。通过访问此平台,您确认已阅读并同意遵守我们的使用条款和隐私政策,这些条款和政策旨在保护您的体验和隐私权。

[email protected]

关于我们使用条款隐私政策
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) 版权所有。