1
00:00:00,000 --> 00:00:03,990
Python has some very powerful facilities for working with strings.

2
00:00:04,000 --> 00:00:09,990
And in fact, in Python 3, they become refined, and in some cases even more powerful.

3
00:00:10,000 --> 00:00:15,990
Let's get right into strings, and we'll start by making a working copy of variables.py.

4
00:00:16,000 --> 00:00:20,990
And we can call this variables-strings.py,

5
00:00:21,000 --> 00:00:23,990
go ahead and open that working copy,

6
00:00:24,000 --> 00:00:26,990
and we'll start by defining a String.

7
00:00:27,000 --> 00:00:33,990
I'll just call it s, and I'll say 'This is a string!' and we'll go ahead, and

8
00:00:34,000 --> 00:00:40,990
we'll print(s) and save that and run it.

9
00:00:41,000 --> 00:00:41,990
And there we have the string.

10
00:00:42,000 --> 00:00:46,990
Strings in Python are immutable objects,

11
00:00:47,000 --> 00:00:51,990
and they're created with either single quotes or double quotes.

12
00:00:52,000 --> 00:00:54,990
And so if we change those single quotes to double quotes, we'll see - if we

13
00:00:55,000 --> 00:01:00,990
save and run, we'll see that we get exactly the same result.

14
00:01:01,000 --> 00:01:05,990
You can use Escape characters in a string, as you can in many languages.

15
00:01:06,000 --> 00:01:11,990
So if I put in here a \n, that'll introduce a new line in the middle of the string.

16
00:01:12,000 --> 00:01:16,990
And if I save that and run it, we see that there is a new line now between the a

17
00:01:17,000 --> 00:01:18,990
and the s of string.

18
00:01:19,000 --> 00:01:23,990
On the other hand, if I want this \n to actually be a part of the string, rather

19
00:01:24,000 --> 00:01:28,990
than getting replaced with a new line, I can put the letter r before the

20
00:01:29,000 --> 00:01:31,990
definition of the string like that,

21
00:01:32,000 --> 00:01:36,990
and if I save that and run it, I get the \n as part of the string.

22
00:01:37,000 --> 00:01:41,990
This is called a raw string, and the place where this gets used the most is when

23
00:01:42,000 --> 00:01:43,990
creating regular expressions.

24
00:01:44,000 --> 00:01:48,990
And we'll get into regular expressions in some detail in another chapter.

25
00:01:49,000 --> 00:01:54,990
In addition to these sorts of Escapes with the backslashes, you can also do some

26
00:01:55,000 --> 00:01:58,990
formatting and replacing of variables in the string.

27
00:01:59,000 --> 00:02:05,990
For example, if I create a number and call this, say n, and give it a value of

28
00:02:06,000 --> 00:02:11,990
42, I could put that 42 right in the middle of the string here.

29
00:02:12,000 --> 00:02:15,990
And I'm going to show you the Python 3 way to do this.

30
00:02:16,000 --> 00:02:20,990
And we'll look briefly at the Python 2 way to do it as well, because you'll see that a lot.

31
00:02:21,000 --> 00:02:24,990
For all your new code, you want to use the Python 3 way, which is with the

32
00:02:25,000 --> 00:02:31,990
format method of the string object, because the Python 2 way is going to go away.

33
00:02:32,000 --> 00:02:36,990
It's considered obsolescent, and it will be dropped in the next version of Python.

34
00:02:37,000 --> 00:02:38,990
So I'm going to save this and run.

35
00:02:39,000 --> 00:02:39,990
And you'll see what this does.

36
00:02:40,000 --> 00:02:45,990
This actually inserts the value of n in the middle of the string.

37
00:02:46,000 --> 00:02:53,990
We have these curly braces here, and those get replaced with the format.

38
00:02:54,000 --> 00:02:58,990
So format is a method of the string object.

39
00:02:59,000 --> 00:03:01,990
And so this literal string is actually an object.

40
00:03:02,000 --> 00:03:04,990
Remember, everything in Python is an object.

41
00:03:05,000 --> 00:03:09,990
We can use this objecty-referencing operator, this period, to access a method

42
00:03:10,000 --> 00:03:14,990
of that object, and do this variable replacement.

43
00:03:15,000 --> 00:03:17,990
So this is very powerful, and this is very common.

44
00:03:18,000 --> 00:03:21,990
And you'll see it done this way in new Python 3 code.

45
00:03:22,000 --> 00:03:23,990
In Python 2 code, you'll see it's done this other way.

46
00:03:24,000 --> 00:03:35,990
I'm going to put %s here, and over here a % sign and the letter n. And so we'll

47
00:03:36,000 --> 00:03:38,990
save that and we'll run it.

48
00:03:39,000 --> 00:03:40,990
And you see we get the same result.

49
00:03:41,000 --> 00:03:43,990
This is the way that it was done in Python 2.

50
00:03:44,000 --> 00:03:45,990
And this is a bit of a hack.

51
00:03:46,000 --> 00:03:48,990
It's perfectly valid, and you'll see it a lot.

52
00:03:49,000 --> 00:03:52,990
You'll even see it some in Python 3 code that's written by people who are used

53
00:03:53,000 --> 00:03:55,990
to Python 2, and that's fine.

54
00:03:56,000 --> 00:03:59,990
The reason you don't want to use this construct is because it is considered

55
00:04:00,000 --> 00:04:04,990
obsolescent, and it will be dropped in the next version of Python.

56
00:04:05,000 --> 00:04:09,990
So you'll see this, you want to know what it does, but the right way to do it in

57
00:04:10,000 --> 00:04:19,990
Python 3 is to use the new format method of the string object.

58
00:04:20,000 --> 00:04:20,990
And that will look like this.

59
00:04:21,000 --> 00:04:22,990
I'll save that and run it.

60
00:04:23,000 --> 00:04:24,990
And that's how that works.

61
00:04:25,000 --> 00:04:28,990
There's one more way I want to show you for defining a string, and this is

62
00:04:29,000 --> 00:04:30,990
using the triple quotes.

63
00:04:31,000 --> 00:04:33,990
And you can triple either the single quotes or the double quotes.

64
00:04:34,000 --> 00:04:41,990
So I can either do it this way, or instead of these triple quotes, I can do it

65
00:04:42,000 --> 00:04:44,990
this way with the double quotes, like this.

66
00:04:45,000 --> 00:04:46,990
And you'll see it's done both ways.

67
00:04:47,000 --> 00:04:50,990
I tend to use the singles tripled instead of the doubles tripled.

68
00:04:51,000 --> 00:04:57,990
Now what this does is it allows you to have a string that spans several lines.

69
00:04:58,000 --> 00:05:05,990
And so, what I'll often do is I'll start with a backslash and a new line and go

70
00:05:06,000 --> 00:05:08,990
all the way back to the end of the line like that,

71
00:05:09,000 --> 00:05:19,990
and this will allow me to just have line after line of text and more text,

72
00:05:20,000 --> 00:05:20,990
and to have it actually started at the beginning of it.

73
00:05:21,000 --> 00:05:22,990
I'll explain what this does in a moment.

74
00:05:23,000 --> 00:05:23,990
Let's save this and run.

75
00:05:24,000 --> 00:05:25,990
And then you can see what this does.

76
00:05:26,000 --> 00:05:29,990
This is useful if you have lines and lines and lines of text.

77
00:05:30,000 --> 00:05:35,990
And so the way you do this is with three quotes, either single quotes or double

78
00:05:36,000 --> 00:05:36,990
quotes, at the beginning and the end.

79
00:05:37,000 --> 00:05:40,990
And the result will be a string that has the new lines, and it's all formatted

80
00:05:41,000 --> 00:05:42,990
exactly like you type it in.

81
00:05:43,000 --> 00:05:48,990
What this here does, this backslash and a new line, is that it escapes the new

82
00:05:49,000 --> 00:05:52,990
line, so that it doesn't actually show up in the string.

83
00:05:53,000 --> 00:06:00,990
If I didn't have this, and I save this and run it - that's scrolled out of the way -

84
00:06:01,000 --> 00:06:03,990
there you see that we get a black line at the beginning, because this blank line

85
00:06:04,000 --> 00:06:06,990
here is actually inserted in the string.

86
00:06:07,000 --> 00:06:10,990
The way to get that not to happen is to use the backslash before it.

87
00:06:11,000 --> 00:06:14,990
The backslash has to be the very last character on the line, so that it's escaping

88
00:06:15,000 --> 00:06:17,990
the new line and not a space or something.

89
00:06:18,000 --> 00:06:19,990
So I save that and I run it,

90
00:06:20,000 --> 00:06:21,990
and you can see that we don't have a scrollbar over here.

91
00:06:22,000 --> 00:06:24,990
This is actually at the top,

92
00:06:25,000 --> 00:06:26,990
and there is no new line at the beginning of the string.

93
00:06:27,000 --> 00:06:30,990
So that's the triple quote way of defining strings.

94
00:06:31,000 --> 00:06:35,990
And that's often used in docstrings in function, which we'll get to later on in

95
00:06:36,000 --> 00:06:46,000
the chapter on functions.

