Locked lesson.
About this lesson
What is a string?
Exercise files
Download this lesson’s related exercise files.
Strings.docx58.9 KB Strings - Solution.docx
58.9 KB
Quick reference
Strings
Let's learn more about strings! A string is a sequence of characters.
When to use
Strings are something that you're going to use all the time, whenever you write code.
Instructions
Strings can be wrapped in single or double quotes.
You can use the escape character "\" to print a quotation mark to the screen.
\n creates a line break.
Hints & tips
- Strings are wrapped in single or double quotes
- \ is an escape character
- \n means new line
- 00:04 In this video, I want to talk about strings in a little bit more detail.
- 00:08 And so let's create a string.
- 00:09 Let's just call it my_string.
- 00:11 This is a terrible name for a string but it will work for us here.
- 00:14 So, I've mentioned in the past that strings can be
- 00:17 inside these double quotation marks or single quotation marks.
- 00:20 But we haven't actually talked about what the difference between the two is, or
- 00:24 why you might want to use one set over another.
- 00:27 And that's what I want to talk about right now.
- 00:29 So as we know, we can print this out just by going, my_string.
- 00:34 So let's say we had a string and let's say inside here, we said, we typed in,
- 00:39 The teacher said, "PAY ATTENTION!" and
- 00:44 we wanted this "PAY ATTENTION!" to be printed out inside quotation marks.
- 00:48 Well, if we do it like this, if we run this, we're going to get an error,
- 00:53 you can see.
- 00:53 Because what's going on here is, we're starting with the quotation mark, and
- 00:57 Python is looking for everything inside of these quotation marks and
- 01:01 when it sees the next set of quotation marks, it stops.
- 01:03 And then everything after here, it doesn't know what to do with it,
- 01:07 because it thinks that this is the string and then it's just confused.
- 01:11 So in this case, you might want to wrap your string in single quotation marks.
- 01:17 So when you use single quotation marks as the outer wrapping,
- 01:21 you can use double quotation marks inside.
- 01:23 And see now, Python says okay, we're starting with a single quote.
- 01:26 I'm going to keep looking until I find the next single quote.
- 01:29 It goes all the way to the end.
- 01:30 It ignores those two double quotes and it sees the single quote.
- 01:34 So now if we save this, we can run it and it says, The teacher said,
- 01:38 "PAY ATTENTION!".
- 01:39 And the PAY ATTENTION! is in nice quotation marks.
- 01:42 Likewise, we can reverse this as well.
- 01:44 We can put the double quotes on the outside and
- 01:47 the single quotes on the inside.
- 01:52 Just like that, if we save this, we're going to get the same exact thing,
- 01:55 teacher says 'PAY ATTENTION!', only now, it has these single quotes on the inside.
- 01:59 So, that's really sort of the main reason why you're going to choose single over
- 02:04 double, or vice versa.
- 02:05 It's if you're inside of here using these things.
- 02:08 Now, there's another way you could do it as well.
- 02:10 If we want double quotes on the outside and the inside,
- 02:13 we can use something called an escape character.
- 02:15 And that's just the backslash.
- 02:17 And you can see, once we do that,
- 02:18 this quotation mark turns blue and we need to do another one right there.
- 02:22 What this is doing is it's escaping out.
- 02:24 It's saying okay, the next character you see, just ignore it.
- 02:27 Just print it out, don't run it as a string type thing,
- 02:30 just kind of print it out as text.
- 02:32 So if we save this and
- 02:33 run it, now it works again because of the escape characters.
- 02:37 So we can use double quotes inside and outside and the same thing holds true for
- 02:41 the single quotes as well.
- 02:42 So that's an escape character.
- 02:43 There is also another kind of neat thing you can do with an escape character.
- 02:47 You can do n, n stands for new line,
- 02:50 so if you want, you see, this is all on one line.
- 02:53 In fact we could come back here and we could say \n.
- 02:58 We save this and run it, the teacher said PAY ATTENTION! and
- 03:02 it's put this PAY ATTENTION! on a new line.
- 03:05 So that \n stands for.
- 03:07 So escape character, new line and single quotes, double quotes.
- 03:11 Really those are the main things you want to learn about strings.
- 03:14 So you're going to use strings just all the time.
- 03:17 I mean, they're like the most fundamental building block of any
- 03:20 programming language, and Python is no different.
- 03:21 So get good and used to creating your own strings.
- 03:24 Use double quotes or single quotes.
- 03:26 A lot of the time it's just a matter of style.
- 03:28 You might always use double quotes or you might always use single quotes.
- 03:32 Sometimes like we've seen in this video, you'll need to use single or
- 03:36 double quotes, but for the most part you just use whichever feels right to you.
- 03:40 Personally, I use double quotes, just because I do.
- 03:43 But like I said, it's really just a matter of style.
- 03:45 So in the next video, we're going to look at string manipulation.
Lesson notes are only available for subscribers.