Computer Science, asked by Fghi3761, 11 months ago

What do you understand by traversing a string ? Ans. Traversing a string means accessing all the elements of the string one after the other by using the subscript. A string can be traversed using for loop or while loop.
For example :
A = ‘Python’
i = 0
while i < lenn (A) :
print A[i]
i = i + 1
Output :
P
y
t
h
o
n

Answers

Answered by mad210219
7

Traversing a string

Explanation:

We can traverse each character in a string using the slicing and indexing feature of python

Without indexing feature of list or aray we can not able to count charecters in a string or find length of a string

We can also traverse all the charecters in a string by using for loop also  

So ,

Showing how to traverse using for loop in python

Lets discuss

For example:

A = ‘Python’  is a string  

We access all the charecters now

Observe the program using for loop

A=’PYTHON’

for i in range(len(A)):

     print(A[i])

Output:

P

Y

T

H

O  

N

Here for loop traverse from 0 to len(A)-1

Len(0) function calculates the length of string in python .

Answered by Anonymous
1

Answer:

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.vv

Similar questions