I tried and I only printed out the letter 's's instead of the words that start with s. I'm not sure how to use the .split() function

552 views Asked by At

Use for, .split(), and if to create a Statement that will print out words that start with 's':

st = 'Print only the words that start with s in this sentence'

2

There are 2 answers

0
eisa.exe On

Split the list into Strings with .split(). Use " " as a separator because you want spaces to separate the words.

st = "Print only the words that start with s in this sentence"
words = []
for word in st.split(" "):
    if word.startswith("s"):
        words.append(word)

print(words)
0
Akash Saha On

Try this instead

st = 'Print only the words that start with s in this sentence'
Answer = [word for word in st.split() if word[0] == 's']
print(Answer)