Loop


#1

https://www.codecademy.com/courses/learn-python/lessons/loops/exercises/for-your-a?action=resume_content_item

phrase = “A bird in the hand…”

Add your for loop

for char in phrase:
if char in [“A”,“a”]:
print “X”,
else:
print char

#Don’t delete this print statement!
print

the output of the above code is as shown below:

X
b
i
r
d

i
n

t
h
e

h
X n
d
.
.
.

here I have excluded the Comma(,) after Char that’s why it is printing like this. as we can see , n is printed next to X… the same way b must get print after X in the very first line itself.

could anyone let me know the reason of it ??


#2

if we look at the phrase:

A bird in the hand…

the A at the beginning of the sentence and the next character (a space) are printed on the same line, if we change the phrase slightly:

phrase = "A_bird in the hand…"

then run the loop, we should see:

X_
b
i
r
d