Answers for "index of python"

63

get index of list python

list.index(element)
Posted by: Guest on February-09-2020
41

python find index by value

>>> ["foo", "bar", "baz"].index("bar")
1
Posted by: Guest on November-19-2019
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
7

python index of element in list

# alphabets list
alphabets = ['a', 'e', 'i', 'o', 'g', 'l', 'i', 'u']

# index of 'i' in alphabets
index = alphabets.index('i')   # 2
print('The index of i:', index)

# 'i' after the 4th index is searched
index = alphabets.index('i', 4)   # 6
print('The index of i:', index)

# 'i' between 3rd and 5th index is searched
index = alphabets.index('i', 3, 5)   # Error!
print('The index of i:', index)
Posted by: Guest on December-29-2020
17

python index method

my_string = "Hello World!"

my_string.index("l") # outputs 2
# this method only outputs the index of the first "l" value in the string/list
Posted by: Guest on June-07-2020
-1

index of python

# Python is a 0-based indexing language, meanign all indices start from 0
list1 = ["Python", "Is", "A", "0", "Based", "Indexing", "language"]
print(list1[0]) # Prints Python.... the element on index 0, 1 in human counting
Posted by: Guest on January-16-2021

Python Answers by Framework

Browse Popular Code Answers by Language