English subtitles

← Finding Strings in Strings - Intro to Computer Science

Get Embed Code
3 Languages

Showing Revision 6 created 05/25/2016 by Udacity Robot.

  1. So I want to introduce one more operation on strings, which
  2. we'll find very useful, which is the Find operation. It gives
  3. us the way in a big string to find some sub-string
  4. that we're looking for. The way we use Find is a little
  5. different from the way we've used other operators so far. Because
  6. Find is actually a method, and what that means is it's
  7. a built in procedure provided by Python. We'll be able to
  8. define our own procedures soon, we'll get to that in unit two.
  9. Find is a procedure that operates on strings, so we use it by having
  10. a string followed by .find, followed by
  11. a parentheses, then we pass in another string.
  12. Which is the string that we want to find in
  13. the first string. And the output of Find is the
  14. position in the string where that sub-string is found, the
  15. first occurrence of the string. So, if that string happens
  16. to occur in more places than one in the
  17. input string, the result of find is always going to give
  18. us the position. That's the number where the first occurrence
  19. of the sub-string occurs. So the output of using Find
  20. will be the first position in the search string, which
  21. is this blue string right here, where the target string,
  22. which is the purple string, occurs. So that will be
  23. a number. If the target string is not a found anywhere
  24. in the search string, then the output would be negative
  25. 1. So let's try a few examples to understand how
  26. that works and we'll do this in the Python interpreter.
  27. Here I've initialized the variable Pythagoras to hold the string here
  28. that's been attributed to Pythagoras. We don't know if
  29. he really said it. But it says there's a geometry
  30. in the humming of strings, there is music in
  31. the spacing of spheres. So now, we have that variable
  32. initialized, so I'm going to invoke Find, using Pythagoras as
  33. the string that we're searching in, and that's the value
  34. that we initialized it to with a string, passing in
  35. as the search string the string string. When we run
  36. this, we see that we get 40 as the result. If we counted, this is position 0, we
  37. would see string starting at position 40. Since I
  38. don't want to count that far, we can use our indexing
  39. to see if that's right. So let's print Pythagoras
  40. starting from index 40, we could print all the
  41. way to the end using a colon. And, when
  42. we run that, we see that it starts with string
  43. which is what we found with the Find. We can search
  44. for other positions if we search for Pythagoras the single letter T.
  45. Well that matches the beginning, so we should find the resulted position
  46. 0 which is what we get and we can look for sphere.
  48. That will match sphere at the end. We get
  49. position 86. Let's print the quote from position 86. And
  50. we see the end of the quote starting from sphere. [SOUND]
  51. If we search for a string that's not in the string
  52. that we're using as the search string, so let's look for
  53. say, algebra, which was not in the quote from Pythagoras, we
  54. get the output negative one. That means the string was not found.