In this article we will discuss different ways to iterate or loop over all the characters of string in forward, backward direction and also by skipping over certain characters.

Iterate over string using for loop

Iterating over the string is simple using for loop and in operator i.e.

sampleStr = "Hello!!"

print("**** Iterate over string using for loop****")

for elem in sampleStr:
    print(elem)

Output:
**** Iterate over string using for loop****
H
e
l
l
o
!
!

Iterate over string with index using range()

range(len (stringObj) ) function will generate the sequence from 0 to n -1 ( n is size of string) .  Now iterate over this sequence and for each index access the character from string using operator [] i.e.

print("**** Iterate over string with index using range() ****")    

for i in range( len(sampleStr) ):
    print(sampleStr[i])

It will print the string contents
**** Iterate over string with index using range() ****
H
e
l
l
o
!
!

Iterate over a portion of string only

To iterate over a portion of string like a sub string , we can use slicing operator to generate a sub string and then iterate over that sub string. To generate a slice we will use [] operator i.e.

string[start : stop : step size]

We can pass the start and stop index to generate a sub string and then we can iterate over it. For example, lets see how to iterate over the first 3 characters of string

# Iterate over the first three elements of string
for elem in sampleStr[0:3:1] : 
    print(elem)

Output:
**** Iterate over a portion of string only ****
H
e
l

Iterate over a string by skipping characters

print("**** Iterate over string  by skipping every 2nd characters ****")    

# Iterate over a string with 2 characters at a time
for elem in sampleStr[ : : 2] : 
    print(elem)

Output:
**** Iterate over string  by skipping every 2nd characters ****
H
l
o
!

Iterate over string in backward / reverse direction using slicing

In the slicing operator [] i.e.

string[start: stop : step size]

If we skip the start and stop index, then it will use the whole string for generating sub string. But if we pass step size as -1 then it will iterate over the current string in backward direction to generate a new string, that is the reverse of original string. Then iterate over this reversed string i.e.

print("**** Iterate over string in reverse using slice operation****")    

for elem in sampleStr[ : :-1]:
    print(elem)

It will print the string contents in reverse order :
**** Iterate over string in reverse using slice operation****
!
!
o
l
l
e
H

Iterate over string in reverse using indexing :

Instead of generating a new reversed string and then iterating over that we can iterate over the current string in backward direction using indexing i.e.

print("**** Iterate over string in reverse****")    

i = len(sampleStr) - 1 
while i >= 0 :
    print(sampleStr[i])
    i = i - 1

It will print the string contents in reverse order

or we can use negative indexing to iterate over a string in backward direction i.e.

print("**** Iterate over string in reverse using negative indexing****")    

i = 1 
while i <= len(sampleStr) :
    print(sampleStr[-i])
    i = i + 1    

Output:
!
!
o
l
l
e
H

Complete example is as follows,
def main():
    
    sampleStr = "Hello!!"
    
    print("**** Iterate over string using for loop****")
    
    for elem in sampleStr:
        print(elem)
    
    print("**** Iterate over string with index using range() ****")    
    
    for i in range( len(sampleStr) ):
        print(sampleStr[i])

    print("**** Iterate over a portion of string only ****")
    
    # Iterate over the first three elements of string
    for elem in sampleStr[0:3:1] : 
        print(elem)    
        
        
    print("**** Iterate over string  by skipping every 2nd characters ****")    
    
    # Iterate over a string with 2 characters at a time
    for elem in sampleStr[ : : 2] : 
        print(elem)
    
    print("**** Iterate over string in reverse using slice operation****")    
    
    for elem in sampleStr[ : :-1]:
        print(elem)
        
    print("**** Iterate over string in reverse****")    
    
    i = len(sampleStr) - 1 
    while i >= 0 :
        print(sampleStr[i])
        i = i - 1
    
    print("**** Iterate over string in reverse using negative indexing****")    
    
    i = 1 
    while i <= len(sampleStr) :
        print(sampleStr[-i])
        i = i + 1    
        
        
           
if __name__ == '__main__':
    main()


Output:
**** Iterate over string using for loop****
H
e
l
l
o
!
!
**** Iterate over string with index using range() ****
H
e
l
l
o
!
!
**** Iterate over a portion of string only ****
H
e
l
**** Iterate over string  by skipping every 2nd characters ****
H
l
o
!
**** Iterate over string in reverse using slice operation****
!
!
o
l
l
e
H
**** Iterate over string in reverse****
!
!
o
l
l
e
H
**** Iterate over string in reverse using negative indexing****
!
!
o
l
l
e
H

 

Join a list of 2000+ Programmers for latest Tips & Tutorials