© Paul Gerrard 2016
Paul GerrardLean Python10.1007/978-1-4842-2385-7_10

10. Searching

Paul Gerrard
(1)
Maidenhead, Berkshire, UK
 

Searching for Strings

Searching for text in strings is a common activity and the built-in string function find() is all you need for simple searches. It returns the position (offset) of the find or –1 if not found.
>>> txt="The quick brown fox jumps over the lazy dog"
>>> txt.find('jump')
20
>>> txt.find('z')
37
>>> txt.find('green')
-1

More Complex Searches

There are often circumstances when the search is not so simple. Rather than a simple string, we need to look for a pattern and extract the information we really want from the matched text. Suppose for example, we wanted to extract all the URLs in links on a web page. Here ...

Get Lean Python: Learn Just Enough Python to Build Useful Tools now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.