1
00:00:00,000 --> 00:00:03,990
In Python, strings are objects, just like everything is an object.

2
00:00:04,000 --> 00:00:07,990
We are going to take a little bit of time and look at what this means, because

3
00:00:08,000 --> 00:00:10,990
this is not what people are used to who have been programming in other

4
00:00:11,000 --> 00:00:11,990
programming languages.

5
00:00:12,000 --> 00:00:19,990
But in Python, the "everything is an object" paradigm has particular impact on how

6
00:00:20,000 --> 00:00:21,990
we operate on strings.

7
00:00:22,000 --> 00:00:24,990
And also, it's a little bit different than it was in Python 2.

8
00:00:25,000 --> 00:00:28,990
Even though strings were objects in Python 2, they weren't fully first class

9
00:00:29,000 --> 00:00:32,990
objects in the sense that they are in Python 3, and the interface is a lot more

10
00:00:33,000 --> 00:00:36,990
consistent in Python 3, but it also means that it's significantly different

11
00:00:37,000 --> 00:00:37,990
than it was in Python 2.

12
00:00:38,000 --> 00:00:41,990
So if you're familiar with Python 2, this is worth paying attention to just a little bit.

13
00:00:42,000 --> 00:00:49,990
So here we have a string, 'this is a string,' and the value of that of course is a string.

14
00:00:50,000 --> 00:00:56,990
Now, if we assign it to a variable, now we have a variable, and if we just get

15
00:00:57,000 --> 00:00:58,990
the value of that variable, it says, "This is a string."

16
00:00:59,000 --> 00:01:03,990
Now, of course in Python a variable is just a reference to an object.

17
00:01:04,000 --> 00:01:08,990
So I can operate on that string by saying s.upper() and and we get the

18
00:01:09,000 --> 00:01:09,990
uppercase of the string.

19
00:01:10,000 --> 00:01:13,990
And here is the thing that surprises people sometimes is I can do the same

20
00:01:14,000 --> 00:01:20,990
thing just on the string, I can say 'This is a string'.upper(), and that

21
00:01:21,000 --> 00:01:25,990
will give me the uppercase of THIS IS A STRING., because the string itself is an object.

22
00:01:26,000 --> 00:01:29,990
The significant impact here is the use of the format method.

23
00:01:30,000 --> 00:01:34,990
The format method is very often called on an bare string, so to speak.

24
00:01:35,000 --> 00:01:37,990
So it's very common to do something like this in Python.

25
00:01:38,000 --> 00:01:45,990
This is a string and put in a format, and close the quotes, and say .format(42)

26
00:01:46,000 --> 00:01:50,990
like that, and the result will be 'This is a string 42', because what we have

27
00:01:51,000 --> 00:01:55,990
done is we actually operated on the string using the format method, and

28
00:01:56,000 --> 00:02:02,990
replaced the token in the string with the formatted number 42, and this is very common to do.

29
00:02:03,000 --> 00:02:07,990
Older versions of Python, it was common to do something like, 'This is a string

30
00:02:08,000 --> 00:02:16,990
%d' and then use the % operator and say 42 like that, and that worked exactly

31
00:02:17,000 --> 00:02:22,990
the same way, because you weren't using the dot notation to access a method.

32
00:02:23,000 --> 00:02:27,990
Instead it overloading this % operator to do exactly the same thing.

33
00:02:28,000 --> 00:02:32,990
Now, it's worth noting that in Python 3 that % notation is considered

34
00:02:33,000 --> 00:02:36,990
obsolescent, which means that in future versions of Python that will no

35
00:02:37,000 --> 00:02:37,990
longer be supported.

36
00:02:38,000 --> 00:02:41,990
So this is a good time to get used to using the .format method, and in

37
00:02:42,000 --> 00:02:47,990
fact, the format method is a lot more powerful and a lot more consistent in its syntax.

38
00:02:48,000 --> 00:02:52,990
The syntax used in the old percent style string replacement was borrowed from C,

39
00:02:53,000 --> 00:02:56,990
and it's 40 years old, and while it certainly works and it's certainly familiar

40
00:02:57,000 --> 00:02:58,990
to anybody with the background in the C programming language,

41
00:02:59,000 --> 00:03:03,990
it's not as consistent or as powerful as the new format operator in the format language.

42
00:03:04,000 --> 00:03:07,990
So we will be talking more about that in detail later on in this chapter.

43
00:03:08,000 --> 00:03:12,990
For now, what's important to note is that a string is an object and a

44
00:03:13,000 --> 00:03:16,990
variable containing a string is really just a variable that contains a

45
00:03:17,000 --> 00:03:17,990
reference to a string object.

46
00:03:18,000 --> 00:03:21,990
So everything that you can do on that string object, you can do on the string

47
00:03:22,000 --> 00:03:32,000
itself, because the string itself is the string object.

