World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Neha Kumawat
a year ago
text = 'Python Forensics - indexing'
result = text.index('in')
print("Keyword found at",result)
Keyword found at 19
str.index(sub, start, end)
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)
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
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))
28
30
Traceback (most recent call last):
File "main.py", line 10, in <module>
print(str1.index('is', 19, 30))
ValueError: substring not found
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))
Index for number: 1
Index for pointing: 3
Index of the keyword found at 13