Search Python string with index method

Search a string

We can use index method to find a substring. It has the following syntax.

s.index(sub [, start [, end]])

Like find, but raises ValueError if not found instead of returning -1.


searchStr =  "Red Blue Violet Green Blue Yellow Black"
print searchStr.index("Blue")

The code above generates the following result.

Search from 20th character.


searchStr =  "Red Blue Violet Green Blue Yellow Black"
print searchStr.index("Blue",20)




















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary