First loop prints each letter of hospital on a line by incrementing the the counter per letter. Strings start at position 0
#!/usr/bin/env python
place=”hospital”
length=len(place)
print(‘hospital has’,length,’letters’)
counter=0
print(”)
print(‘print each letter at a time’)
while place[counter:]:
print(place[counter])
counter+=1
hospital has 8 letters
print each letter at a time
h
o
s
p
i
t
a
l
This is created by a nested while loop. Each loop starts at 0 and increments to the length of hospital. When outside loop increments it seeds the number for the inside loop to begin with. i must be less than j. If i=j, it will print a blank line.
print(‘slicing strings’)
i=0
while i <= length:
j=i
while j <= length:
print(place[i:j])
j+=1
i+=1
print(”);
slicing strings
h
ho
hos
hosp
hospi
hospit
hospita
hospital
o
os
osp
ospi
ospit
ospita
ospital
s
sp
spi
spit
spita
spital
p
pi
pit
pita
pital
i
it
ita
ital
t
ta
tal
a
al
l
I added if i not equal to j, to not print the blank. I added the print(i, ‘ ‘j) to show you what prints at each increment.
i=0
while i <= length:
j=i
while j <= length:
if i!=j:
print(i,’ ‘,j)
print(place[i:j])
else:
print(i,’ ‘,j)
print(”)
j+=1
i+=1
print(”);
0 0 #prints nothing
0 1
h
0 2
ho
0 3
hos
0 4
hosp
0 5
hospi
0 6
hospit
0 7
hospita
0 8
hospital
1 1 #prints nothing
1 2
o
1 3
os
1 4
osp
1 5
ospi
1 6
ospit
1 7
ospita
1 8
ospital
2 2 #prints nothing
2 3
s
2 4
sp
2 5
spi
2 6
spit
2 7
spita
2 8
spital
3 3 #prints nothing
3 4
p
3 5
pi
3 6
pit
3 7
pita
3 8
pital
4 4 #prints nothing
4 5
i
4 6
it
4 7
ita
4 8
ital
5 5 #prints nothing
5 6
t
5 7
ta
5 8
tal
6 6 #prints nothing
6 7
a
6 8
al
7 7 #prints nothing
7 8
l
8 8 #prints nothing
Process finished with exit code 0