Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In Python, strings are objects, just like everything is an object. We are going to take a little bit of time and look at what this means, because this is not what people are used to who have been programming in other programming languages. But in Python, the "everything is an object" paradigm has particular impact on how we operate on strings. And also, it's a little bit different than it was in Python 2. Even though strings were objects in Python 2, they weren't fully first class objects in the sense that they are in Python 3, and the interface is a lot more consistent in Python 3, but it also means that it's significantly different than it was in Python 2.
So if you're familiar with Python 2, this is worth paying attention to just a little bit. So here we have a string, 'this is a string,' and the value of that of course is a string. Now, if we assign it to a variable, now we have a variable, and if we just get the value of that variable, it says, "This is a string." Now, of course in Python a variable is just a reference to an object. So I can operate on that string by saying s.upper() and and we get the uppercase of the string.
And here is the thing that surprises people sometimes is I can do the same thing just on the string, I can say 'This is a string'.upper(), and that will give me the uppercase of THIS IS A STRING., because the string itself is an object. The significant impact here is the use of the format method. The format method is very often called on an bare string, so to speak. So it's very common to do something like this in Python. This is a string and put in a format, and close the quotes, and say .format(42) like that, and the result will be 'This is a string 42', because what we have done is we actually operated on the string using the format method, and replaced the token in the string with the formatted number 42, and this is very common to do.
Older versions of Python, it was common to do something like, 'This is a string %d' and then use the % operator and say 42 like that, and that worked exactly the same way, because you weren't using the dot notation to access a method. Instead it overloading this % operator to do exactly the same thing. Now, it's worth noting that in Python 3 that % notation is considered obsolescent, which means that in future versions of Python that will no longer be supported.
So this is a good time to get used to using the .format method, and in fact, the format method is a lot more powerful and a lot more consistent in its syntax. The syntax used in the old percent style string replacement was borrowed from C, and it's 40 years old, and while it certainly works and it's certainly familiar to anybody with the background in the C programming language, it's not as consistent or as powerful as the new format operator in the format language. So we will be talking more about that in detail later on in this chapter.
For now, what's important to note is that a string is an object and a variable containing a string is really just a variable that contains a reference to a string object. So everything that you can do on that string object, you can do on the string itself, because the string itself is the string object.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 103154 Viewers
61 Video lessons · 89835 Viewers
71 Video lessons · 73452 Viewers
56 Video lessons · 105000 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.