Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

What Is a String? (Solution)

00:00 Here I am in the interactive IDLE shell. I’ll go ahead and take quick notes of the different tasks that I’ve got to do here. There were four tasks. I always like to start with just taking notes on what I’m supposed to do, which helps me get organized and remember what the task is actually.

00:18 So the first one was to print a string with double quotes. So I want double quotes in the string. That’s the first task. Then the second one was to have an apostrophe in the string.

00:33 The third one was to have a multiline string that also gets printed on multiple lines. And the fourth one was to write a string in multiline,

00:47 but print it in a single line.

00:51 So I just took some short notes to remind myself of the tasks and I’m going to get going. The first one is to print double quotes in the string, and I can do that using single quotation marks to delimit the string.

01:04 And then I can use double quotation marks inside. I open up a string by putting a single quotation mark. Then I can write something in here. So I will say 'There are "double quotes"

01:18 in this string'. And I surrounded the words double quotes with actual double quotes. And it should print fine, because I’m not mixing the quotation marks.

01:29 I’m using single quotation marks to delimit the string, and then the double quotation marks inside. They’re not considered as string delimiters, but they’re just the characters double quotation marks.

01:41 So now I press Enter, and my print() call executes, and I get as an output the string. Great. Second one, an apostrophe in the string.

01:52 Do another print() call. And now I will use double quotation marks to delimit the string. So I’m opening it up with a double quotation mark. Then I can write something. I will say, "This string's got an apostrophe", and I use an apostrophe after the word string.

02:11 So this string has got an apostrophe. This is not an English lesson, and I wouldn’t be the best person to teach this anyway. But I do have a single quotation mark or an apostrophe in the string, and I can now press Enter, and this will execute and work fine.

02:28 Exercise three was to write a multiline string that also prints on multiple lines. So this I can do by using triple quotation marks. So for example, triple double quotation marks (""") or triple single quotation marks (''') works the same.

02:43 And now I can say """This string was written on multiple lines. Well, not yet. Let me press Enter, and then I can continue with the string and say and it displays across multiple lines.

03:02 And I need to close the string. So I’ll close it again with triple double quotation marks. One, two, three. And then also close the parentheses from the print() function.

03:15 And now I’m ready to press Enter. And this executes. And as you can see, the newline character, so the Enter that I pressed in here, this newline character, is also part of the string.

03:24 So the output includes it, and that’s why it displays on two lines.

03:30 And then finally, the last task, write a multiline string that prints on a single line. And you can do that with escaping the newline character. Now I’m just going to use one double quotation mark.

03:41 And I’ll say, "This one-line string was written and then I will use the backslash character (\), which escapes. It’s a special character that escapes the following character, which in this case is going to be a newline because after putting the backslash character, I’ll press Enter and continue typing in the next line.

04:03 But Python considers this to still be part of the previous line. So continue writing my string saying, using multiple lines" and then close the string. With a string like this one that’s delimited with only one quotation mark, spreading a string over multiple lines like I do here only works when you use the backslash character.

04:26 Otherwise, Python would raise a SyntaxError.

04:30 And now press Enter. And as you can see, it displays on just a single line because I escaped the newline character that I used in here. All right, so that was a quick recap on the different ways you can mix and match quotation marks when writing your string literals.

Become a Member to join the conversation.