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   w  w  w .  j av a  2  s.  co  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 
# from www .j  a va  2s.c o m
for i in range(len(X)): print(X[i], end=' ')  # Manual range/len iteration

Result

Related Topic