YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Selecting Sub Sequences - Intro to Computer Science

Get Embed Code
4 Languages

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

  1. So there are lots more things we can do with
  2. strings. The next one I am going to talk about, is
  3. selecting sub-sequences from strings. So what we have seen so
  4. far, we have used indexing, where we have a string,
  5. where we have our square bracket. We have some expression
  6. that produces one number, and we have our closed square
  7. bracket, and that gives us a one-character string. And whatever
  8. position the number refers to in the string here. The other
  9. thing we can do with square brackets is select
  10. a sub-sequence of the string. Instead of just having
  11. one expression here, what we can have is an
  12. expression. This is also something that should evaluate to a
  13. number, followed by a colon, followed by another expression.
  14. Both of these expressions are numbers. And this will
  15. evaluate to a string, this is a subsequence of
  16. a characters in, the input string. So I called the
  17. string s, the value of this number will call
  18. start, and the value of this number will call
  19. stop. And what the result is, is a string
  20. that's a subsequence of all the characters in s,
  21. and the string that we had here. Starting from
  22. position start, so the number to the left of
  23. the colon, and ending with position stop, but not
  24. including that character. So it's going to really include the characters
  25. from position start, up through stop minus 1. So this construct allows us
  26. to select from any string A subsequence of continuous characters in that string.
  27. So, let's try an example in the Python interpreter.
  28. I'm going to initialize the variable word to be the
  29. string assume. And using the single index operator we
  30. saw initially, we can select a character from word.
  31. So, if I select word index 3. That will
  32. give us the fourth character, zero, one, two, three,
  33. which is u, and when we run that, we
  34. see the result is u. With the new operation,
  35. that's the same as selecting word 3:4,
  36. that's going to select starting from position three, going
  37. to just before position four. So that will end up being just the one letter u.
  38. Just to make this clear, if I selected
  39. 3:3, there's no string there, that's just a
  40. single empty string which prints out in a
  41. way we can't see it. There's no characters
  42. between 3 and character 3. We'll go back to
  43. just selecting index 3. I could select from position 4
  44. through position 6. That would give us a string which
  45. is a subsequence of letters at position 4 and position
  46. 5. So when we run this now we see the
  47. first one prints out to u. The second one prints
  48. out me which is position 4 and position 5. The
  49. last two characters in the word. I'll show you one
  50. other thing we can do. Which is, leave one of
  51. the sides of the colon empty. So it we did 4:
  52. to the end, that will select from position 4 to the
  53. end. We don't need to actually count the characters to know
  54. where the end is. So that will produce the same thing
  55. as we did before. We can also leave nothing in front
  56. of the colon. That will select from the beginning. So if
  57. we do :2, that will select from position 0 up to
  58. position 2, giving us the first two letters of the word, as. And we could
  59. leave both sides blank, selecting word, [:],
  60. with nothing on either side, well, it starts from the beginning, goes to the
  61. end. That's going to give us the whole word. There's
  62. no good reason to ever want to do this, but
  63. it's consistent with the syntax where we can leave
  64. one side of the colon empty and that means
  65. selecting from either the beginning or the end.