1
00:00:04,540 --> 00:00:08,710
In this video, we'll look at three
operators that apply to strings. In fact,

2
00:00:08,710 --> 00:00:10,309
they apply to any sequence type.

3
00:02:13,700 --> 00:02:16,030
Alright, so back to the code. I'm going to create a new Python

4
00:02:16,030 --> 00:02:19,720
file called sequence operators.

5
00:02:28,980 --> 00:02:31,720
So we've already seen the plus operator used for

6
00:02:31,720 --> 00:02:35,800
strings to concatenate them. So let's just
type in a few strings, so I'm gonna start

7
00:02:35,800 --> 00:02:42,489
with typing string1 equals "he's ", with a single quote there, but the

8
00:02:42,489 --> 00:02:47,890
string in double quotes, string2 equals
"probably ", in double quotes, with a space

9
00:02:47,890 --> 00:02:54,730
after the y, string3 equals "pining ",
with a space at the end of the letter g,

10
00:02:54,730 --> 00:03:03,790
string4 is equal to "for the " space, and
double quote there, and then string5 equals double

11
00:03:03,790 --> 00:03:13,150
quotes "fjords", and then I'm going to print
on line 7, string1 + string 2 + string3,

12
00:03:13,150 --> 00:03:23,180
whoops I put a space there, string3
+ string4 + string5.

13
00:03:23,180 --> 00:03:28,720
But the plus isn't necessary when concatenating string literals, though, in Python,

14
00:03:28,720 --> 00:03:39,040
so we could also do print, so on line 9, double quotes he's space, then another space outside the

15
00:03:39,040 --> 00:03:44,280
double quotes, probably, space there,
another closing off double quote,

16
00:03:44,280 --> 00:03:49,240
another set of double quotes but after a space there to separate it, pining space,

17
00:03:49,240 --> 00:03:53,770
whoops,
space outside of the double quote

18
00:03:53,770 --> 00:03:59,950
for the  and space space and Fjord's, not sure if I pronounced that correctly.

19
00:03:59,950 --> 00:04:03,040
Alright,

20
00:04:03,040 --> 00:04:05,920
so that'll print the same thing and
if we run this, we should see both

21
00:04:05,920 --> 00:04:13,720
outputting the identical output, and
obviously I made a typo.

22
00:04:13,720 --> 00:04:15,860
Run it again

23
00:04:15,860 --> 00:04:20,260
and we've got the identical output in both
cases. So I mentioned this second way,

24
00:04:20,260 --> 00:04:23,800
because it's how Python works, not
because we think you'd ever want to do

25
00:04:23,800 --> 00:04:26,290
something like that.
I'm sure you'd agree that the first

26
00:04:26,290 --> 00:04:30,280
example on line 7 is a bit easier to
read. Now with that said, strings can also

27
00:04:30,280 --> 00:04:35,920
be multiplied, which is a bit weird if you're coming from another programming language.

28
00:04:35,920 --> 00:04:41,110
I can come down on line 11, I can
type print parentheses Hello in double

29
00:04:41,110 --> 00:04:47,840
quotes with a space before the closing
double quote.Then I'm going to put * 5.

30
00:04:47,840 --> 00:04:51,940
Now you can't actually perform arithmetic
multiplication on strings, of course.

31
00:04:51,940 --> 00:04:56,050
Instead what happens is, the star or
multiplication operator repeats the

32
00:04:56,050 --> 00:04:59,700
sequence a certain number of times; 5 in
this case.

33
00:04:59,700 --> 00:05:03,800
So if we run the code

34
00:05:03,800 --> 00:05:08,200
you can see we've got the word Hello printed, or outputted, five times.

35
00:05:08,200 --> 00:05:11,980
Now lists and tuples can also
be multiplied, as we'll see when we look

36
00:05:11,980 --> 00:05:14,540
at those sequence types later in the
course.

37
00:05:14,540 --> 00:05:18,520
You can't concatenate or multiply a
range though, and that's not a problem

38
00:05:18,520 --> 00:05:22,390
because you probably wouldn't want to do
that anyway. Now once again, operator

39
00:05:22,390 --> 00:05:28,800
precedence applies, so if I type some
other code here,

40
00:05:28,800 --> 00:05:34,980
if I type print parentheses double quotes Hello, space before the closing double quote,

41
00:05:34,980 --> 00:05:43,140
multiplied by 5 plus 4. So this example wouldn't
print Hello nine times, and again, we're

42
00:05:43,150 --> 00:05:46,330
talking about operator precedence there.
So if we run this program, we'll see that

43
00:05:46,330 --> 00:05:50,520
we'll get an error, and the reason we're
getting an error is because Hello, in

44
00:05:50,520 --> 00:05:55,390
double quotes, x 5 evaluates to a string,
and then there's an attempt to then add

45
00:05:55,390 --> 00:06:00,789
the numeric number 4 to it and it fails
for that reason. So what we can do is, I'm

46
00:06:00,789 --> 00:06:03,250
going to show you two things here, and
they actually do different things.

47
00:06:03,250 --> 00:06:06,550
So firstly, I'm going to change line 13 to put five + four in

48
00:06:06,550 --> 00:06:12,130
parentheses, but then also I can type on
line 14, print parentheses double quotes

49
00:06:12,130 --> 00:06:20,620
Hello space again and multiplied by 5,
plus and 4 in double quotes.

50
00:06:20,620 --> 00:06:26,040
Now because of the parentheses, line 13
evaluates 5 + 4 to get 9, so it should

51
00:06:26,050 --> 00:06:31,300
then repeat the string 9 times. Line 14
should repeat the string 5 times

52
00:06:31,300 --> 00:06:34,750
and then append the string 4 to it, so let's
just run that and confirm that's how it

53
00:06:34,750 --> 00:06:40,240
works, and can see that works, as I
mentioned. Alright, so there's also an

54
00:06:40,240 --> 00:06:44,950
operator to check if one string is a
sub-string of another. To do that,

55
00:06:44,950 --> 00:06:52,000
we actually check if one thing is in
another. So let's start by typing some code.

56
00:06:52,000 --> 00:06:56,940
On line 16 I'm going to type, today
equals Friday, and that's in double quotes

57
00:06:56,940 --> 00:07:01,840
because it's a string.

58
00:07:01,840 --> 00:07:05,720
Okay, next I'm going
to type print parentheses double quotes,

59
00:07:05,720 --> 00:07:11,860
day in today,
and put a comment here for each, which

60
00:07:11,860 --> 00:07:19,570
should be True, print fri in today, and
obviously the first part's in double

61
00:07:19,570 --> 00:07:24,270
quotes, as you can see there. That should
also be True; third example, print

62
00:07:24,270 --> 00:07:32,180
parentheses and we'll put t h u r in
today, which should obviously be False,

63
00:07:32,180 --> 00:07:40,220
and lastly we do a print parentheses parrot in double quotes in double quotes fjord.

64
00:07:40,220 --> 00:07:43,860
That should also be False,

65
00:07:43,860 --> 00:07:50,060
and I'll just clean up
these a little bit.

66
00:07:50,060 --> 00:07:54,780
So here, the in operator evaluates to True if the first
thing exists in the second,

67
00:07:54,780 --> 00:07:59,520
and false otherwise. We'll be using that a lot when we look at conditions and for loops,

68
00:07:59,520 --> 00:08:03,300
and you'll get plenty of practice using it
and see loads of real examples.

69
00:08:03,300 --> 00:08:06,220
So I'm not going to discuss it any further now,
but we'll just run that to confirm we're

70
00:08:06,220 --> 00:08:10,510
getting the results that we think we
should. You can see we get True, True,

71
00:08:10,510 --> 00:08:16,780
False, False, which goes in line with the
code from 17 to 20, and getting the expected values.

72
00:08:16,780 --> 00:08:17,919
Alright so let's end the video here

73
00:08:17,919 --> 00:08:21,280
and in the next one, we're going to look
at the various ways we can format

74
00:08:21,280 --> 00:08:25,740
strings, and some of the string methods
that allow us to manipulate them.

75
00:08:25,740 --> 00:08:28,640
See you in the next video.

