Answers for "python index of string in list"

2

python get index of substring in liast

index = [idx for idx, s in enumerate(l) if 'tiger' in s][0]
Posted by: Guest on October-07-2020
13

python string indexing

str = 'codegrepper'
# str[start:end:step]
#by default: start = 0, end = len(str), step = 1
print(str[:]) 		#codegrepper
print(str[::]) 		#codegrepper
print(str[5:]) 		#repper
print(str[:8]) 		#codegrep
print(str[::2]) 	#cdgepr
print(str[2:8]) 	#degrep
print(str[2:8:2]) 	#dge
#step < 0 : reverse
print(str[::-1]) 	#reppergedoc
print(str[::-3]) 	#rpgo
# str[start:end:-1]	means start from the end, go backward and stop at start
print(str[8:3:-1]) 	#pperg
Posted by: Guest on September-12-2020
2

find the index of a character in a string python

my_var = 'mummy'. #Find the position of 'm'

#Using find - find returns the index for the first instance from the left.
my_var.find('m')
# Output: 0

#Using rfind - rfind returns the index for the first instance from the right.
my_var.rfind('m')
# Output: 3
# With find() and rfind(), when substring is not found, it returns -1.

#NB: You can use index() and rindex(). In this case, when the substring is not
# found, it raises an exception.
Posted by: Guest on August-12-2021
6

get index of item in list

list.index(element, start, end)
Posted by: Guest on June-30-2020
0

python get index of substring in liast

def index_containing_substring(the_list, substring):
    for i, s in enumerate(the_list):
        if substring in s:
              return i
    return -1
Posted by: Guest on October-07-2020
-1

python string index of

sentence = 'Python programming is fun.'

result = sentence.index('is fun')
print("Substring 'is fun':", result)

result = sentence.index('Java')
print("Substring 'Java':", result)
Posted by: Guest on May-30-2020

Code answers related to "python index of string in list"

Python Answers by Framework

Browse Popular Code Answers by Language