Advertisement
diegomrodrigues

Truques no Python - Checando se uma string contém uma substr

Mar 19th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. '''
  2. Truques no Python - Checando se uma string contém uma substring
  3.  
  4. Artigo no LinkedIn:
  5. https://www.linkedin.com/pulse/truques-python-checando-se-uma-string-contém-diego-mendes-rodrigues/
  6.  
  7. Curso de Python 3:
  8. https://produto.mercadolivre.com.br/MLB-991504015-curso-de-python-3-_JM
  9.  
  10. Diego Mendes Rodrigues
  11. '''
  12.  
  13. string = 'Olá mundo'    # Exemplo True
  14. # string = 'Até mais'   # Exemplo False
  15.  
  16. # Forma 'convencional'
  17. if string.find('Olá') != -1:
  18.     print('Successo! Olá é uma substring!')
  19.  
  20. # Forma 'elegante' utilizando 'if  substring in  string'
  21. if 'Olá' in string:
  22.     print('Successo! Olá é uma substring!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement