Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Python 3 equivalent of find ()?

Tags:

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

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.

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

like image 396
AlphaTested Avatar asked Jul 10 '11 22:07

AlphaTested


People also ask

What is find () in Python?

The find() method finds the first occurrence of the specified value. The find() method returns -1 if the value is not found. The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found. ( See example below)

How do I find a word in a string Python 3?

String find() in Python Just call the method on the string object to search for a string, like so: obj. find(“search”). The find() method searches for a query string and returns the character position if found. If the string is not found, it returns -1.

How do I find a character in a string Python 3?

Python 3 - String find() Method The find() method determines if the string str occurs in string, or in a substring of string if the starting index beg and ending index end are given.


1 Answers

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

like image 102
Wooble Avatar answered Oct 11 '22 16:10

Wooble