using isinstance() function In Python

Python3




# create a list of names and marks
list1 = ['sravan', 98, 'harsha', 'jyothika',
        'deepika', 78, 90, 'ramya']
print(list1)
# iterate through list of elements
for i in list1:
 
    # check for type is str
    if(isinstance(i, str)):
     
        # display index
        print(list1.index(i))


Output

['sravan', 98, 'harsha', 'jyothika', 'deepika', 78, 90, 'ramya']
0
2
3
4
7

Time complexity:O(n)

Auxiliary Space:O(n)



Python – Find Index containing String in List

Given a list, the task is to write a Python Program to find the Index containing String.

Example:

Input: [‘sravan’, 98, ‘harsha’, ‘jyothika’, ‘deepika’, 78, 90, ‘ramya’]

Output: 0 2 3 4 7

Explanation: Index 0 2 3 4 7 contains only string.

Similar Reads

Method 1: Using type() operator in for loop

By using type() operator we can get the string elements indexes from the list, string elements will come under str() type, so we iterate through the entire list with for loop and return the index which is of type string....

Method 2: Using type() operator in List Comprehension

...

Method 3: Using isinstance() function

By using list comprehension we can get indices of string elements....

Method 4: Using enumerate() function

...

Method 5: using isinstance() function

Python3 # create a list of names and marks list1 = ['sravan', 98, 'harsha', 'jyothika',         'deepika', 78, 90, 'ramya']   # display print(list1)   list2=[]   # iterate through list of elements for i in list1:     # check for type is str     if(isinstance(i,str)):         # display index         list2.append(list1.index(i)) print(list2)...