English subtitles

← Strings - Intro to Computer Science

Get Embed Code
4 Languages

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

  1. So, so far all the computation we've done
  2. is operated only on numbers, and in the
  3. early days of computing, people thought of computers
  4. as super powerful calculators, for doing computations like
  5. simulating nuclear weapons, computing ballistic tables, or breaking
  6. encryptions, which was a little more than just
  7. arithmetic. But still was mostly about counting and
  8. doing simple arithmetic. We saw this quote from
  9. Grace Hopper earlier talking about computers could only do arithmetic, and
  10. this is what people thought about computers in the 1940s and
  11. 1950s. But there's no reason to limit computers to that. They
  12. can operate on any kind of data we want, and it
  13. gets much more interesting when we operate on data besides just
  14. numbers. If we're going to build a search engine, most of the
  15. data that we want to deal with is not numbers. It's
  16. the letters that are contained in webpages, and in Python, that's what
  17. we call a string. A string is just a sequence of
  18. characters surrounded by quotes. So here's an example of a string
  19. in Python, it starts with a single quote, has a sequence
  20. of characters and anything that we can type on the keyboard can
  21. be in a string, and ends with another single quote. The
  22. string is the sequence of characters between the single quotes. If
  23. we want, we can use double quotes instead. If we use
  24. double quote, then the double quote starts the string. We can have
  25. a sequence of characters and a double quote that ends the
  26. string. The only requirement is that if we start the string
  27. with a single quote, it has to end with a single
  28. quote. If we start the string with a double quote, it
  29. has to end with a double quote. And that's actually a
  30. handy property. Because that means we can have the other kind
  31. of quote within our string. This string starts with a double
  32. quote. It contains a single quote inside it. But because we started
  33. with a double quote, that single quote doesn't end the string.
  34. That single quote is just like another character in the string.
  35. The string continues. Until the closing double quote. So let's try
  36. some things in the Python interpreter. So, we can print a
  37. string, just like we can print a number. So here I'm
  38. printing the string hello. And when we run this, we see
  39. the output hello. It's printing hello. We don't see the single
  40. quotes as it prints, but we know that it's a string that
  41. was printed. We can print a string with double quotes,
  42. and one thing to notice when you enter strings through the
  43. interpreter, the color is black now, since it's an open
  44. string, it hasn't been finished, once I type the final quote,
  45. that closes the string, the color changes to blue. So
  46. now when we run this, we have two prints, both that
  47. print hello, it looks the same both times. It doesn't matter
  48. if we use single quotes or double quotes around our string.
  49. Just to check everyone's paying attention, I'm going to try one
  50. more thing. And now I am printing hello without the quotes.
  51. You can guess what that will do. We won't make a
  52. quiz of this, but try to guess before I run it.
  53. Now that I run this, we see the result.
  54. We get a name error. The name Hello is not
  55. defined. Without the quotes, this looks just like a variable.
  56. It's a name, but it's a variable that we didn't
  57. define. So when I try to use it, I get
  58. an error that the variable name Hello is not defined.
  59. If I wanted to I could define a variable named
  60. hello. Let's make hello refer to the string howdy and
  61. now when I print hello, it works. I see the first
  62. two prints that printed hello, now when I print the variable hello,
  63. well that refers to the string howdy and I see howdy as
  64. the result. We usually don't want our variables to start with capital
  65. letters. That's just a convention, so I'm going to change this
  66. back to a lower case hello because it makes me feel uncomfortable
  67. to have a variable with a capital letter. There's no rule against
  68. that in Python. It's just a convention that we like to follow.