1
00:00:05,180 --> 00:00:09,300
The backslash character has got a
special meaning in strings.

2
00:00:09,300 --> 00:00:14,259
It's used to escape the character that follows it, to give that character special meaning.

3
00:00:14,260 --> 00:00:21,000
Now some examples are backslash n, to start a new line, and backslash t to insert a tab.

4
00:00:21,000 --> 00:00:24,060
So let's go ahead and create a new Python file.

5
00:00:24,060 --> 00:00:36,200
I'm going to call this one escapechar.py

6
00:00:36,200 --> 00:00:42,860
Alright, so start by typing splitString is equal to,

7
00:00:42,860 --> 00:00:51,860
and in double quotes, This string has been, and we'll put a backslash n there, and type split over.

8
00:00:51,860 --> 00:00:57,860
Then backslash n several backslash n lines,

9
00:00:57,860 --> 00:01:05,060
and to confirm what this looks like, we're going to type print, and in parenthesis, splitString, like so.

10
00:01:05,060 --> 00:01:09,860
Now before I run this, remember to check what your IDE thinks you want to run.

11
00:01:09,860 --> 00:01:13,240
Now we haven't executed this code before, so we have to right click in the editor to run it from there,

12
00:01:13,240 --> 00:01:17,229
and you can see in the top right hand corner,

13
00:01:17,229 --> 00:01:19,920
it's going to run strings if we just click on this button here,

14
00:01:19,920 --> 00:01:21,960
which is not what we think should happen.

15
00:01:21,960 --> 00:01:27,280
So I'm just going to close that off. I'll  just type in the answer there, close that off.

16
00:01:27,280 --> 00:01:31,000
So again, what we need to is right-click and select run.

17
00:01:31,000 --> 00:01:34,760
Alright, and now we can see those backslash characters and what they're doing.

18
00:01:34,760 --> 00:01:39,380
You can see how the string has been indeed spit over several lines in the output.

19
00:01:39,380 --> 00:01:42,999
So whenever the print function sees backslash n in a string,

20
00:01:43,000 --> 00:01:45,120
it causes it to start a new line.

21
00:01:45,120 --> 00:01:48,780
Many IDEs will color escape characters differently in the code,

22
00:01:48,780 --> 00:01:54,940
as IntelliJ IDEA is doing on my screen. You can see that on line 1, the various backslash ns there.

23
00:01:54,940 --> 00:01:58,740
Alright, so that was backslash n. We can also tab the output.

24
00:01:58,740 --> 00:02:05,800
So using backslash t causes Python to tab to the next stop before printing. So let's go ahead and try that.

25
00:02:05,800 --> 00:02:20,280
So I'm going to type tabbedString is equal to, and in double quotes I'm going to type 1\t2\t3\t4, you guessed it, \t5.

26
00:02:20,280 --> 00:02:28,320
Alright and then print that out to print and in parentheses, tabbedString this time, and we'll run this.

27
00:02:28,320 --> 00:02:33,280
We can see the numbers 1 to 5 are printed out, tabbed across the screen.

28
00:02:33,280 --> 00:02:40,060
Now different consoles have different tab stops. In IDEA, the tab stops are every four characters.

29
00:02:40,060 --> 00:02:43,100
But if you run this program from, say, a terminal or a command prompt,

30
00:02:43,100 --> 00:02:47,840
the tab stops will probably be set to eight characters, so keep that in mind.

31
00:02:47,840 --> 00:02:53,180
So the backslash can also be used to escape special characters, such as quotes and double quotes,

32
00:02:53,180 --> 00:02:57,160
and that can be useful if they you've got a string containing both of those characters.

33
00:02:57,160 --> 00:02:59,760
So to see what I mean, let's type some more code.

34
00:02:59,760 --> 00:03:04,220
So I'm going to type print parenthesis and single quote,

35
00:03:04,220 --> 00:03:14,840
and I'm going to type The pet shop owner said, and then double quote, No, no, and a space, we're gonna put a

36
00:03:14,840 --> 00:03:24,480
backslash single quote, bear with me here, e backslash single quote again, s then r.

37
00:03:24,480 --> 00:03:33,440
Hopefully you're recognizing the reference here, and three periods, he backslash single quote s resting,

38
00:03:33,440 --> 00:03:39,480
and closing double quote, a period and a single quote which is already there, and the right parenthesis.

39
00:03:39,480 --> 00:03:41,860
So that's one option, or what we can do,

40
00:03:41,860 --> 00:03:44,564
is we can come back and also try another version of that,

41
00:03:44,564 --> 00:03:51,180
and we can put, and I'll put a comment there, hash or. We can do it another way print, and put a double quote this time,

42
00:03:51,180 --> 00:03:58,260
The pet shop owner said, space backslash double quote

43
00:03:58,260 --> 00:04:06,200
No, no, space single quotes e, and we're putting the e in single quotes there,

44
00:04:06,200 --> 00:04:13,500
s for e's uh comma three periods, he's resting.

45
00:04:13,500 --> 00:04:16,760
Then we want to put a backslash double quote a period,

46
00:04:16,760 --> 00:04:21,760
then we've got the closing double quote from the start of the line, and the right parentheses.

47
00:04:21,760 --> 00:04:25,360
Let's just run this first.

48
00:04:25,360 --> 00:04:31,240
So you can see now that what the pet shop owner said is in double quotes, but his statement includes apostrophies,

49
00:04:31,240 --> 00:04:33,460
and that means that we have to escape one or the other.

50
00:04:33,460 --> 00:04:36,960
So on line 7, the string is delimited with single quotes, as you saw,

51
00:04:36,960 --> 00:04:40,080
and we have to escape any single quotes that appear in the string.

52
00:04:40,080 --> 00:04:44,760
However, on line 9 what we've done, is we've delimited using double quotes,

53
00:04:44,760 --> 00:04:48,740
and that means the double quotes inside the string must be escaped.

54
00:04:48,740 --> 00:04:53,080
Now another alternative is when you use
triple quotes, which we'll do now,

55
00:04:53,080 --> 00:04:58,080
there's no need to escape any quotes inside the string. So I'm going to type print

56
00:04:58,080 --> 00:05:03,940
left parenthesis and three double quotes, and noting that IntelliJ added the last three for us automatically,

57
00:05:03,940 --> 00:05:10,200
and I can type The pet shop owner said,

58
00:05:10,200 --> 00:05:17,520
then a double quote. Notice we're not using the backslash now for escape characters, no, no, single quote,

59
00:05:17,520 --> 00:05:27,300
e single quote, s for e's uh semicolon three periods, he single quote he's resting.

60
00:05:27,300 --> 00:05:32,880
Then we've got our double quote to close off the quote, from the start of No, no, he's uh, ... he's resting

61
00:05:32,880 --> 00:05:38,020
and we want to put a period there, and we'll just put a space and the three double quotes on the right-hand side.

62
00:05:38,020 --> 00:05:40,740
Close off the print line with the right parenthesis.

63
00:05:40,740 --> 00:05:46,000
So basically, when we're using triple quotes there, there's no need to escape any quotes inside the string.

64
00:05:46,000 --> 00:05:51,300
Python knows that the string doesn't end, until it finds those matching triple quotes at the end.

65
00:05:51,300 --> 00:05:54,340
And just to confirm this we'll run it,

66
00:05:54,340 --> 00:05:57,080
and as you can see, we've got exactly the same output.

67
00:05:57,080 --> 00:05:59,960
So another reason we might want three consecutive quotes

68
00:05:59,960 --> 00:06:05,100
is because of a feature Python provides, to make strings spanning lines more readable.

69
00:06:05,100 --> 00:06:11,080
So the string that we split over several lines earlier, is not easy to read because of the embedded backslash ns.

70
00:06:11,080 --> 00:06:16,640
but Python allows the string to be split over several lines without doing that, by using triple quotes

71
00:06:16,640 --> 00:06:19,320
So to see what I mean, let's type some code in.

72
00:06:19,320 --> 00:06:28,920
We'll do this on line 13. So I'm going to type anotherSplitString is equal to, three double quotes,

73
00:06:28,920 --> 00:06:39,640
This string has been, I'm pressing enter there, split over, enter, several, enter, lines.

74
00:06:39,640 --> 00:06:47,000
Then on the next line, which is the line after, just to make it clear, and I'm gonna print another string, anotherSpitString.

75
00:06:47,000 --> 00:06:50,460
If we run that,

76
00:06:50,460 --> 00:06:53,780
and you can see This string has been split over several lines.

77
00:06:53,780 --> 00:06:56,120
So we're basically getting line breaks in the output.

78
00:06:56,120 --> 00:07:01,660
Now if you want to lay out a string with breaks, but don't want every one to start with a new line when printed,

79
00:07:01,660 --> 00:07:05,740
then what you can do is escape the end of a line with a backslash.

80
00:07:05,740 --> 00:07:07,580
So to see what I mean there,

81
00:07:07,580 --> 00:07:13,540
we can come to the end of the line. We'll
come up here to line 13, has been been space, we'll put a backslash there.

82
00:07:13,540 --> 00:07:19,240
On the next line, do the same, space backslash several backslash after the space,

83
00:07:19,240 --> 00:07:22,240
and now if we run this, you'll see the difference in the output.

84
00:07:22,240 --> 00:07:25,140
The string has been split over several lines, is actually no longer true.

85
00:07:25,140 --> 00:07:30,520
It's now all on the same line because we've used the backslash to escape the end of the line.

86
00:07:30,520 --> 00:07:32,500
Alright so let's finish the video here.

87
00:07:32,500 --> 00:07:35,180
See you in the next video.

