Sep-13-2018, 01:32 PM
I have set out to use a for loop using indexing to print out only the words that start with an s in this sentence: "Secret agents are super good at staying hidden."
Here is the code I wrote:
To explain this code, here I take the string and split each word up. Then I iterate or cycle through each item. If the first letter of each item is an ‘S’ or ‘s’, only that word should be printed.
Therefore, I was expecting this output:
But instead I got:
Can someone explain better what my code is actually doing and what I need to do to produce the expected output?
For my reference, this forum post refers to Task #1 in Jose Portilla’s so called 04-Field-Readiness-Exam-2 in his “Narrative Journey” Udemy Python course material (on GitHub: Pierian-Data/Python-Narrative-Journey).
Here is the code I wrote:
1 2 3 4 5 |
mystring = "Secret agents are super good at staying hidden." mystring.split() for i in mystring: if mystring[ 0 ] = = 'S' or 's' : print (i) |
Therefore, I was expecting this output:
Quote:Secret
super
staying
But instead I got:
Quote:S
e
c
r
e
t
a
g
e
n
t
s
a
r
e
s
u
p
e
r
g
o
o
d
a
t
s
t
a
y
i
n
g
h
i
d
d
e
n
Can someone explain better what my code is actually doing and what I need to do to produce the expected output?
For my reference, this forum post refers to Task #1 in Jose Portilla’s so called 04-Field-Readiness-Exam-2 in his “Narrative Journey” Udemy Python course material (on GitHub: Pierian-Data/Python-Narrative-Journey).