All Courses

Python Forensics - Indexing

Neha Kumawat

9 months ago

Python Forensics - Indexing | Insideaiml
Table of Contents
  • Introduction
  • index() Syntax
  • index() Parameters
  • index() Return Value

Introduction

            Indexing actually provides the investigator to have a complete look at a file and gather potential evidence from it. The evidence could be contained within a file, a disk image, a memory snapshot, or a network trace.
Indexing allows us to search for a keyword and do an interactive search with the index to find keywords quickly. We can also use it to list the keywords in an ordered list.
Indexing also helps in listing the keywords in a sorted list.
The index () method returns the index of a substring within the string (if found). If the substring is not found, an exception is thrown.
Example
text = 'Python Forensics - indexing'
result = text.index('in')
print("Keyword found at",result)
Output
Keyword found at 19

index() Syntax

It's syntax is:
str.index(sub, start, end)

index() Parameters

The index() method takes three parameters:
  • sub - substring to be searched in the string str.
  • start and end(optional) - substring is searched within str[start:end]

index() Return Value

  • If there is a substring within the string, the lowest index in the string in which the substring is found is returned.
  •    If the substring does not exist within the string, a ValueError exception is thrown.
The index () method is similar to the find () method for strings.
The only difference is that the find () method returns 1 if the substring is not found while index () throws an exception.
Example 1: index() With Substring argument Only
str1= 'Python is a popular programming language'
res = str1.index('is a')
print("Substring 'is a':", res)

res = str1.index('C++')
print("Substring 'C++':", res)
Output
Substring 'is a': 7
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    res = str1.index('C++')
ValueError: substring not found
ValueError: substring not found
Note: Index in Python starts from 0 and not 1. So the occurrence is 7 and not 8.
Example 2: index() With start and end Arguments
str1= 'Python is a popular programming language'

# Substring is searched in 'a popular programming language'
print(str1.index('ing', 10))

# Substring is searched in 'a popular programming lang '
print(str1.index('g lan', 10, -4))

# Substring is searched in 'programming'
print(str1.index('is', 19, 30))
Output
28
30
Traceback (most recent call last):
  File "main.py", line 10, in <module>
    print(str1.index('is', 19, 30))
ValueError: substring not found
Example 3 : Find the index of the item form list
The following example shows how you can use indexing in Python.
List1 = [999, 'number', 'name', 'pointing'];

print("Index for number: ", List1.index('number'))
print("Index for pointing: ", List1.index('pointing'))

str1 = "integer is a number data type"
str2 = "number";

print ("Index of the keyword found at ", str1.index(str2))
The above script will produce the following output.
Index for number:  1
Index for pointing:  3
Index of the keyword found at  13
Enjoyed reading this blog? Then why not share it with others. Help us make this AI community stronger. 
To learn more about such concepts related to Artificial Intelligence, visit our insideAIML blog page.
You can also ask direct queries related to Artificial Intelligence, Deep Learning, Data Science and Machine Learning on our live insideAIML discussion forum.
Keep Learning. Keep Growing. 

Submit Review