Traversing & Accessing a String


☞It can be done in two ways :

1. Using Index
2. Without Using Index

Using Index

Example 1 :
 
#Traversing a string using for loop
S = "PYTHON"
for i in range(len(S)):
    print(S[i])   
    
Output :
 
P
Y
T
H
O
N   
    
Example 2 :
 
#Traversing a string using while loop
S = "PYTHON"
i = 0
while i < len(S):
    print(S[i])
    i = i + 1   
    
Output :
 
P
Y
T
H
O
N   
    

Without Using Index

Example :
 
S = "PYTHON"
for i in S:
    print(i)   
    
Output :
 
P
Y
T
H
O
N