Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. s = "Hello Python!"
  2.  
  3. print(s.find('H'))
  4. # output: 0
  5.  
  6. # if more than one occurance, return the first index
  7. print(s.find('o'))
  8. # output : 4
  9.  
  10. # we can search from the end instead of the beginning
  11. print(s.rfind('o'))
  12. # output : 10
  13.  
  14. # or we can start searching from a particular location
  15. print(s.find('o', 5)) # start searching from index 5, i.e. the whitespace
  16. # output : 10
  17.  
  18. # search substring
  19. print(s.find('Pyt'))
  20. # output : 6
  21.  
  22. # if not found
  23. print(s.find('x'))
  24. # output : -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement