什么是Python 3相当于find()?

时间:2022-05-20 20:30:51

I'm working on the MIT open courseware for python but have having a hard time with the following example:

我正在研究用于python的麻省理工学院开放课件,但是在下面的例子中遇到了困难:

To get started, we are going to use some built-in Python functions. To use these functions, include the statement from string import * at the beginning of your file. This will allow you to use Python string functions. In particular, if you want to find the starting point of the first match of a keyword string key in a target string target you could use thefind function. Try running on some examples, such as find("atgacatgcacaagtatgcat","atgc") Note how it returns the index of the first instance of the key in the target. Also note that if no instance of the key exists in the target, e.g, find("atgacatgcacaagtatgcat","ggcc") it returns the value -1.

首先,我们将使用一些内置的Python函数。要使用这些函数,请在文件开头包含string import *中的语句。这将允许您使用Python字符串函数。特别是,如果要在目标字符串目标中找到关键字字符串键的第一个匹配的起点,可以使用find函数。尝试运行一些示例,例如find(“atgacatgcacaagtatgcat”,“atgc”)注意它如何返回目标中键的第一个实例的索引。另请注意,如果目标中不存在密钥实例,例如find(“atgacatgcacaagtatgcat”,“ggcc”),则返回值-1。

The course in python 2.4 (or so) but I'm trying to do the assignments in Py3.. learning the differences along the way.

python 2.4(或左右)中的课程,但我正在尝试在Py3中完成作业..学习沿途的差异。

3 个解决方案

#1


28  

Use the .find() method of a string, rather than string.find(). (This also works, and is probably preferable, in python 2).

使用字符串的.find()方法,而不是string.find()。 (这也很有用,在python 2中可能更好)。

#2


9  

Isn't it still just find? From the documentation:

是不是还在找?从文档:

str.find(sub[, start[, end]])

Return the lowest index in the string where substring sub is found, such that sub is contained in the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

返回找到substring sub的字符串中的最低索引,这样sub包含在切片s [start:end]中。可选参数start和end被解释为切片表示法。如果未找到sub,则返回-1。

#3


3  

str = "Python"

In Python2:

string.find(str,"y")

In Python3:

str.find("y")

#1


28  

Use the .find() method of a string, rather than string.find(). (This also works, and is probably preferable, in python 2).

使用字符串的.find()方法,而不是string.find()。 (这也很有用,在python 2中可能更好)。

#2


9  

Isn't it still just find? From the documentation:

是不是还在找?从文档:

str.find(sub[, start[, end]])

Return the lowest index in the string where substring sub is found, such that sub is contained in the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

返回找到substring sub的字符串中的最低索引,这样sub包含在切片s [start:end]中。可选参数start和end被解释为切片表示法。如果未找到sub,则返回-1。

#3


3  

str = "Python"

In Python2:

string.find(str,"y")

In Python3:

str.find("y")