Python ProgrammingPython Programming

How do I iterate over a string in Python?

# Example 1
test_str = "Canada"
for i, c in enumerate(test_str):
        print(i, c)
 
print("------------------------")
 
# Example 2
indx = 0
while indx < len(test_str):
        print(indx, test_str[indx])
        indx += 1
 
print("------------------------")
# Example 3
for char in test_str:
        print(char)
Sample output of above program.

0 C
1 a
2 n
.......
d
a