CBSE Class 11 : Python Strings 9

Iterating Through String

Using for loop we can iterate through a string.
Example:

n="computer"
for i in n:
	print(i)
	

Output:

c
o
m
p
u
t
e
r

n="computer"
for i in n:
	print(i,end=' ')

Output:

c o m p u t e r

 

Traverse a string using while loop

n="computer"
i=0
while i<len(n):
	print(n[i])
	i=i+1

Output:

c
o
m
p
u
t
e
r

 

n="computer"
i=0
while i<len(n):
	print(n[i],end=' ')
	i=i+1

Output:

c o m p u t e r