Traversing a String in Python

Traversing a String

Traversing a String refers to going through the characters of the string.Basically,Traversing is the iteration of all the characters present in the string one by one.There are various ways to traverse or iterate a string like traversing a string using Subscript.Some of the common ways of traversing the string are discussed as follows :

Traversing a String using subscript

1. Traversing a String using ‘for’ Loop 

We can iterate or traverse over a string using a ‘for’ loop.A ‘for’ loop is basically used to iterate or traverse a sequence which includes Tuples,Dictionary,Strings,etc. Basically,’for’ is used as a keyword  in Python programming language i.e. Keywords are special reserved words that have specific meanings and purposes and can’t be used for anything but some specific purposes.

For instance : Take a string and apply for loop in that string .We get :

 

Str1=" This is a new place for meera to adapt "
for a in Str1:
 print(a)

 

   After running the program.the output should be :

T
h
i
s
 
i
s
 
a
 
n
e
w
 
p
l
a
c
e
 
f
o
r
 
m
e
e
r
a
 
t
o
 
a
d
a
p
t

In Python, if an object is iterable ,then it can be looped over using a ‘for’ loop.

2. Traversing a String using ‘while’ Loop

We can iterate or traverse over a string using a ‘while’ loop.We provide the string’s length using the len() function for iterating over a string. In the while loop, the upper limit is passed as the string’s length, traversed from the beginning.Basically,’while’ is also used as a keyword in Python programming language i.e. Keywords are special reserved words that have specific meanings and purposes and can’t be used for anything but some specific purposes.

For instance : Take a string and apply while loop in that string .We get :

Str2=" QWERTYASDFGZXCVB "
count= len(Str2)
i= 0
while i < count :
    print("At index",i,"=",Str2[i])
    i=i+1

After running the program.the output should be :

At index 0 = 
At index 1 = Q
At index 2 = W
At index 3 = E
At index 4 = R
At index 5 = T
At index 6 = Y
At index 7 = A
At index 8 = S
At index 9 = D
At index 10 = F
At index 11 = G
At index 12 = Z
At index 13 = X
At index 14 = C
At index 15 = V
At index 16 = B
At index 17 = 

In Python, if an object is iterable ,then it can be looped over using a ‘while’ loop.

Traversing a String using ‘for’ Loop with the range() Function

We could iterate a string using range () function also.Basically,’for’ Loop with the range() Function is used to execute a block of code or statement repeatedly for a fixed number of times. Range() function is used to perform an action across the specific number of times.

For example : Take a string and apply for loop along with range() function .We get :

String= "ABCDEFGHIJKL"
count= len(String)
i= 0
for i in range(count):
    print("At index",i,"=",String[i])
    i=i+1

After running the program.The output should be :

At index 0 = A
At index 1 = B
At index 2 = C
At index 3 = D
At index 4 = E
At index 5 = F
At index 6 = G
At index 7 = H
At index 8 = I
At index 9 = J
At index 10 = K
At index 11 = L

In this way,you could traverse or iterate a string using a ‘for’ loop or ‘while’ loop along with using range() function.

 

 

%d bloggers like this: