Python 中搜索方法的主要问题是它们没有针对速度进行优化。 这会显着减慢您的程序。
The search method in python can be used to find a substring in a given string. This method returns the index of the first occurrence of the substring in the string. If the substring is not found, it returns -1.
Example:
str = "Hello world"
print str.search("world")
Output: 6
搜索
在 Python 中,您可以使用内置的搜索功能来查找数据序列中的特定项目。 搜索函数有两个参数:要搜索的序列和要查找的关键字或模式。
要搜索其中包含单词“cat”的所有项目,您可以使用以下代码:
打印(“正在搜索‘猫’……”)search =“猫”打印(“找到 %d 项。”%(len(搜索)))
此代码的输出将是:
正在搜索“猫”…找到 5 个项目。
方法
Python 中有许多不同的方法。 下面列出了一些最常用的方法。
def:这是一个接受一个或多个参数的函数。 该函数将返回一个值。
print:这会将文本字符串打印到屏幕上。
if:这测试条件是否为真。 如果是,则执行if块内的代码,否则不执行。