Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created June 29, 2020 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/e0987c9feca9579cd0987fe20724fafb to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/e0987c9feca9579cd0987fe20724fafb to your computer and use it in GitHub Desktop.
s1="HelloWorld!"
#Returns lowest index
print (s1.find("o"))#Output:4
print (s1.index("o"))#Output:4
#Returns highest index
print (s1.rfind("o"))#Output:6
print (s1.rindex("o"))#Output:6
#When substring is not found
#Returns -1
print (s1.find("z"))#Output:-1
print (s1.rfind("z"))#Output:-1
#Raises ValueError
#print (s1.index("z"))#Output:ValueError: substring not found
#print (s1.rindex("z"))#Output:ValueError: substring not found
#start and end index is mentioned
print (s1.find("o",3,5))#Output:4
print (s1.rfind("o",3,5))#Output:4
print (s1.index("o",3,5))#Output:4
print (s1.rindex("o",3,5))#Output:4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment