python get index of substring in liast
index = [idx for idx, s in enumerate(l) if 'tiger' in s][0]
python get index of substring in liast
index = [idx for idx, s in enumerate(l) if 'tiger' in s][0]
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
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.
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
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)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us