4.2 index() and find() method - Python
Tags: python, beginners
Published: 2019-12-23 20:13:15

### Indexing in Python string
Strings are ordered sequences of character data. Indexing allows you to access individual characters in a string directly by using a numeric value. String indexing is zero-based: the first character in the string has index 0, the next is 1, and so on. In this post, I’ll show you  string indexing syntax and practice with several examples
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/n27d6uhbzyvizwr4eobx.png)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/1wr90mjnil60r7226bgq.png)

From the above figure-
1. first character in the string has index 0, the next is 1, and so on.
2. If start indexing from the last character then you have to start with -1 and as you come near to first character negative value increases by 1 each time. this is also called negative indexing.

### How to access a character in string:

Simple answer is by using the index of all the characters in the string.

Example:
```python
s='Hello'
#if i want to access 'H' in Hello then 
print("First character is : "s[0])      # indexing stated from 0

# if i want to access 'o' in s
print(" Last character using positive indexing : ",s[4])

#another way
print("last charcater using negative indexing : ",s[-1])    # using negative indexing
```
Output is:
```python
First character is : H
Last character using positive indexing : o
last charcater using negative indexing : o
```

**If we pass a index value which not exist for that string then it will return an error**
```python
s='python'
print("Third character is : ",s[2])
print("Seven character is : ",s[7])    # which is not possible for given string
```
Output gives an error for the second value
```python
Third character is : t
Seven character is : 
IndexError: string index out of range
```

###**Python String index() Method**

1. The **index()** method finds the first occurrence of the specified value.
2. The **index()** method raises an exception if the value is not found.

**Syntax**
```python
     string.index(value, start, end)
```
**value(Required.) -**The string to be searched for.

**start(Optional.)-** This function specifies the position
from where search has to be started. Default is 0

**end(Optional.) -** This function specifies the 
position from where search has to end. Default is to the end of the string

Return the starting index number of the passed valued according to the start and end value passed.

By default start value is 0 and end value is string length.

Example
```python
sentence='hello world and hello python'
value='h'
x=sentence.index(value)   # start and end is default value
print(x)

y=sentence.index(value,2)  # end value is default
print(y)

z=sentence.index(value,6,8)   # give error because in between 6 and 8 'h' not exist.
print(z)

value='and'
a=sentence.index(value,6,20)
print(a)
```
Output is :
```python
0
16
ValueError: substring not found
12
```


###**Python String find() Method**

1. The **find()** method finds the first occurrence of the specified value.
2. The **find()** method returns -1 if the value is not found.
3. 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.
4. If value is not found **find()** method return -1.

**Syntax**
```python
string.find(value, start, end)
```
**value(Required.) -**The string to be searched for.

**start(Optional.)-** This function specifies the position
from where search has to be started. Default is 0

**end(Optional.) -** This function specifies the 
position from where search has to end. Default is to the end of the string

Example:
```python
sentence='hello world and hello python'
value='h'
x=sentence.index(value)   # start and end is default value
print(x)

y=sentence.index(value,2)  # end value is default
print(y)

z=sentence.index(value,6,8)   #  Retuen -1 because value not found between 6 and 8.
print(z)

value='and'
a=sentence.index(value,6,20)
print(a)
```
Output is :
```python
0
16
-1
12
```