Python - Sequence Scans via while, range, for

Description

Sequence Scans via while, range, for

Demo

X = 'test' 
for item in X: print(item, end=' ')           # Simple iteration

Result

You can do it with a while loop:

Demo

X = 'test' 
i = 0 # from  ww  w  .ja v a  2s .  c o  m
while i < len(X):                             # while loop iteration 
     print(X[i], end=' ') 
     i += 1

Result

You can also do manual indexing with a for.

Demo

X =  'test' 
print( len(X) )                                        # Length of string 
print( list(range(len(X))) )                           # All legal offsets into X 
#  w  w w .  j a v a  2 s  . co  m
for i in range(len(X)): print(X[i], end=' ')  # Manual range/len iteration

Result

Related Topic