Write a python program to Find Index Using Enumerate

Write a python program to Find Index Using Enumerate

Program
def myfun():
    str1 = 'Sateesh'
    for index, value in enumerate(str1):
        if value =='t':
            print(f"The index of t is {index}")
                 
if __name__=="__main__":
    myfun()


Output:

The index of t is 2


Program
def myfun():
    str1 = 'Sateesh'
    for i, j in enumerate(str1):
        print(f"Index: {i}, Value: {j}")
                 
if __name__=="__main__":
    myfun()


Output:

Index: 0, Value: S
Index: 1, Value: a
Index: 2, Value: t
Index: 3, Value: e
Index: 4, Value: e
Index: 5, Value: s
Index: 6, Value: h


Program
If you don’t want to use a for loop, you can use enumerate with the list function, and it will return a list of tuples. Each tuple will have a value and its index.
def myfun():
    str1 = 'Sateesh'
    str_counta = list(enumerate(str1))
    print(str_counta)
                 
if __name__=="__main__":
    myfun()


Output:

[(0, 'S'), (1, 'a'), (2, 't'), (3, 'e'), (4, 'e'), (5, 's'), (6, 'h')]



More Questions


53 . Write a python program to Find Index Using Enumerate
54 . Write a python program to checking if a string is empty or not ?
55 . Write a python program to merge nested lists ?
56 . Write a python program to Checking if a File Exists or Not ?
57 . Write a python program to convert uppercase strings and we want to convert them into lowercase strings and remove duplicates ?
58 . Write a python program to display list of numbers and we want to return all the numbers from the list that are divisible by 2 and remove duplicates at the same time.
59 . Write a python program to calculate average using python *args ?
60 . Write a Python program to find the area of a triangle.
61 . Write a Python program to convert kilometers to miles.
62 . Write a Python program to convert Celsius to Fahrenheit.
63 . Write a Python Program to Check if a Number is Positive, Negative or Zero.



For latest job updates join Telegram Channel: https://t.me/sateeshm