Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MoisesTedeschi/62ff6c9c92825ab893b78df586b8d75a to your computer and use it in GitHub Desktop.

Select an option

Save MoisesTedeschi/62ff6c9c92825ab893b78df586b8d75a to your computer and use it in GitHub Desktop.
Use for, split() e if para criar uma declaração que imprima as palavras que começam com 's'...
st = 'Print only the words that start with s in this sentence'
# Resolvendo de forma normal
new_st_normal = st.split()
for char_normal in new_st_normal:
if char_normal[0] == 's':
print(char_normal)
# Resolvendo com List Comprehensions
new_st = [char for char in st.split() if char[0] == 's']
print(new_st)
@MoisesTedeschi
Copy link
Copy Markdown
Author

  • O código pode ser mais reduzido usando a expressão já dentro do print.
    print([char for char in st.split() if char[0] == 's'])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment