1

00:00:00,850  -->  00:00:07,640
Welcome to lesson 19 which roughly covers pages 123 to 127 of the automate the boring stuff with life

2

00:00:07,640  -->  00:00:13,890
on textbook and text is one of the most common forms of data that your programs will handle.

3

00:00:13,890  -->  00:00:18,600
You already know how to concatenate two string values together with a plus operator but Python can do

4

00:00:18,600  -->  00:00:20,570
much more than that.

5

00:00:20,580  -->  00:00:26,740
Let's take a look at some of the ways Python lets you write print and access strings in your code.

6

00:00:26,940  -->  00:00:29,860
Toping string values in Python code is fairly straightforward.

7

00:00:29,910  -->  00:00:33,190
They begin and end with a single quote.

8

00:00:33,570  -->  00:00:37,430
But then how can you use a single quote that's inside of a string.

9

00:00:37,440  -->  00:00:42,750
For example we have the string that is Alice's cat.

10

00:00:42,810  -->  00:00:48,930
This doesn't work because Python thinks that the string ends after Alice because of this quote and that

11

00:00:48,930  -->  00:00:54,510
the rest this s cat is just invalid Python code.

12

00:00:54,930  -->  00:00:57,910
Fortunately there are multiple ways to type strings.

13

00:00:57,930  -->  00:01:03,050
Strings can begin and end with double quotes just as they do with single quotes.

14

00:01:03,060  -->  00:01:05,520
That is Alice's cat.

15

00:01:05,880  -->  00:01:10,670
One benefit of using double quotes that a string can have a single quote character in it.

16

00:01:10,680  -->  00:01:16,290
Since the string begins with a double quote Python is smart enough to know that a single quote is just

17

00:01:16,290  -->  00:01:20,420
part of the string and it's not indicating the end of the string.

18

00:01:20,430  -->  00:01:25,200
However if you need to use both single quotes and double quotes inside of a string you'll need to use

19

00:01:25,320  -->  00:01:31,050
escape characters in escape character lets you use characters that are otherwise impossible to put into

20

00:01:31,050  -->  00:01:37,140
a string escape character consists of a backslash followed by a letter that represents which escape

21

00:01:37,140  -->  00:01:39,010
character you want to add to the string.

22

00:01:39,030  -->  00:01:45,300
For example the escape character for a single quote is backslash quote so I could have a string say

23

00:01:45,330  -->  00:01:50,560
hi to Bob's mother.

24

00:01:50,610  -->  00:01:54,950
Python knows that since this single quote has a backslash before it.

25

00:01:55,080  -->  00:01:58,320
This is not a single quote meant to end the string value.

26

00:01:58,320  -->  00:02:06,360
Instead it's saying put a literal single quote as part of the string table 6 Dash 1 in the automake

27

00:02:06,360  -->  00:02:10,310
book shows the different types of escape characters you'll use.

28

00:02:10,380  -->  00:02:16,830
You can have them for single quotes double quotes if you want have a tab or multiple spaces entered

29

00:02:16,830  -->  00:02:22,990
into your text or if you want to have a new line similar to hitting the enter key.

30

00:02:23,290  -->  00:02:28,320
Or if you want to have a backslash itself you can just have slash slash.

31

00:02:30,130  -->  00:02:33,480
So it's trail that newline character.

32

00:02:33,520  -->  00:02:40,260
Here I'm just going to have something else to print Hello there and then slash in for a newline character

33

00:02:40,270  -->  00:02:40,850
.

34

00:02:41,480  -->  00:02:44,700
Are you another new line.

35

00:02:45,510  -->  00:02:55,800
And then I'm fine escaping that single quote and this so we can have a single print statement.

36

00:02:55,800  -->  00:03:00,130
Print out text across multiple lines.

37

00:03:00,240  -->  00:03:05,190
Now if you have text that includes many backslashes that you don't want seen as the beginning of an

38

00:03:05,190  -->  00:03:11,880
escape character you can use a raw string in Python a raw string is the exact same as a normal string

39

00:03:11,880  -->  00:03:18,280
except it begins with a lowercase are right before.

40

00:03:18,900  -->  00:03:24,360
So this is a valid Python string so we can have something like that is

41

00:03:27,260  -->  00:03:28,660
Karel's cat.

42

00:03:29,010  -->  00:03:35,580
And because this is a raw string this is literally interpreted as part of the string.

43

00:03:35,590  -->  00:03:38,700
You can see when it's printed out as a string value.

44

00:03:38,700  -->  00:03:45,390
Python automatically adds that here let's try copying and pasting this into the print function call

45

00:03:46,240  -->  00:03:49,060
and you can see right now because this is a raw string.

46

00:03:49,200  -->  00:03:55,330
The backslashes are literally a part of the string.

47

00:03:55,330  -->  00:03:59,850
All strings are helpful if you're typing a string value that contains many backslashes and this comes

48

00:03:59,850  -->  00:04:04,170
up in our lesson in the future about regular expressions.

49

00:04:04,170  -->  00:04:10,740
Now while you can use slash in to put a new line into a string it's often easier to just use multiline

50

00:04:10,740  -->  00:04:11,640
strings.

51

00:04:11,640  -->  00:04:18,210
A multi long string in Python begins and ends with either 3 single quote characters or three double

52

00:04:18,210  -->  00:04:19,430
quote characters.

53

00:04:19,590  -->  00:04:26,040
So any quotes tabs or new lines in between the triple quotes are considered part of the string so I

54

00:04:26,040  -->  00:04:27,630
could have something like.

55

00:04:27,690  -->  00:04:28,860
Print.

56

00:04:28,860  -->  00:04:30,610
Begin a new multi line.

57

00:04:31,110  -->  00:04:33,290
Dear Alice heaves.

58

00:04:33,290  -->  00:04:45,190
Cat has been arrested for catnapping cat burglary and extortion.

59

00:04:45,210  -->  00:04:49,310
Sincerely Bob.

60

00:04:49,900  -->  00:04:52,560
And and that multi-line string no notice.

61

00:04:52,890  -->  00:04:56,710
I'm kind of breaking the rules of having one instruction per line right here.

62

00:04:56,760  -->  00:04:58,460
Python's smart enough to realize that.

63

00:04:58,470  -->  00:05:02,260
OK this is all one quote unquote line of code.

64

00:05:02,280  -->  00:05:06,720
This is just one function call doesn't matter that it's being split across multiple lines.

65

00:05:06,720  -->  00:05:11,450
I'm not creating any new blocks or anything like that Python smart enough to know.

66

00:05:11,500  -->  00:05:14,440
OK until I see another triple quotes.

67

00:05:14,550  -->  00:05:19,750
Everything here is part of the string including all the new lines.

68

00:05:20,400  -->  00:05:28,230
So if we stored this string into a variable like spam and we just wanted to display the string content

69

00:05:28,260  -->  00:05:32,710
inside span Python would automatically format it using the slash Hinz.

70

00:05:33,000  -->  00:05:37,290
So triple quotes are really handy if you have some gigantic string.

71

00:05:37,590  -->  00:05:46,620
So I'm going to create a new file editor window and say when to copy and paste the entire text to Shakespeare's

72

00:05:46,770  -->  00:05:53,290
Romeo and Juliet which I happen to have right here in this text file.

73

00:05:53,400  -->  00:05:56,880
I can just hit Control ate slept all and Control-C to copy

74

00:05:59,640  -->  00:06:02,620
and then control V to paste it.

75

00:06:02,950  -->  00:06:07,200
And now any backslashes or single quotes or new lines that are inside.

76

00:06:07,230  -->  00:06:10,840
All of this text are just automatically seen as part of the string.

77

00:06:10,980  -->  00:06:15,610
It's really unlikely that there's going to be a triple double quotes inside of the string and if there

78

00:06:15,780  -->  00:06:19,570
if there are I can just use the triple single quotes instead.

79

00:06:19,570  -->  00:06:30,960
I we just end this so all of this is just one giant variable assignment for the span variable.

80

00:06:32,100  -->  00:06:37,090
And I can just treat that string just like any other string I could print it out.

81

00:06:37,350  -->  00:06:38,770
That's going to be a lot of output though.

82

00:06:38,850  -->  00:06:45,360
Let's just print out with the length of this string is just to see roughly how many characters are in

83

00:06:45,360  -->  00:06:45,510
it.

84

00:06:45,510  -->  00:06:49,820
I'll save this as example that pi and press 5 to run it.

85

00:06:49,840  -->  00:06:54,950
OK so that has 174 thousand letters in it.

86

00:06:55,270  -->  00:06:57,840
Strings use indexes and slices the same way.

87

00:06:57,840  -->  00:07:07,080
List do you can think of a string like hello world as kind of like a list like value with each character

88

00:07:09,570  -->  00:07:12,620
being an item in that list.

89

00:07:13,020  -->  00:07:16,500
So we had something like spam equals Hello world.

90

00:07:16,770  -->  00:07:22,770
I could do all the same things with strings that I do with lists so I can use indexes to pick out a

91

00:07:22,770  -->  00:07:24,360
single letter from it.

92

00:07:24,420  -->  00:07:26,070
I could use slices

93

00:07:30,870  -->  00:07:37,620
to pick out some substring from it I can use the negative indexes to get the very last character the

94

00:07:37,770  -->  00:07:39,490
in and not in operators.

95

00:07:39,630  -->  00:07:40,650
Also work with it.

96

00:07:40,650  -->  00:07:44,660
So hello in spam would evaluate to true.

97

00:07:44,700  -->  00:07:48,030
Since this text does exist inside that string

98

00:07:51,420  -->  00:07:54,990
and see if the letter X appears in that string it doesn't.

99

00:07:55,050  -->  00:07:57,240
So this evaluates to false.

100

00:07:57,330  -->  00:07:59,460
And note that this is case sensitive.

101

00:07:59,460  -->  00:08:06,600
So if I look for capital hello inside that string or turn false.

102

00:08:06,930  -->  00:08:13,800
To recap strings can begin and end with double quotes just as they do single quotes ASCII characters

103

00:08:13,800  -->  00:08:18,850
let you put quotes and other characters that are hard to type inside your string code.

104

00:08:18,870  -->  00:08:25,600
Raw strings will literally print any backslashes in the string and ignore escape characters multi-line

105

00:08:25,680  -->  00:08:33,060
strings begin and end with triple quotes and they can span multiple lines indexes slices the in and

106

00:08:33,060  -->  00:08:36,750
not in operators all work with strings just as they do with list
