Unlike in earlier versions of Python, strings are now first-class objects in Python 3. This lesson shows you how to use object methods on both literal and variable strings.
- Strings are first class objects in Python Three. This is different than many other languages, and it's even a little bit different than Python Two, even though strings were objects in Python Two, they weren't fully first class objects, and the string interface is far more consistent now But change is still change, so if you're familiar with Python Two strings, you'll want to pay close attention here. Here in Komodo, I have opened a working copy of hello.py from chapter 11 of the exercise files, and let's start taking a look at a string as an object.
Here we have a literal string, and even though it's a literal string, I can still call methods on it. So I can call the upper method and when I run this, we get "hello world" in all capital letters. Or I can run the swap case method, and it'll simply swap the case. If it was capital in the original string, it'll make it lowercase in the output string. In fact, it's so common to call methods on a literal string in Python Three, string formatting is commonly done like this.
And I'll put in a placeholder here. And when I run this, I get the result 294, and all of this works with single, double, or even the triple quoted strings that are unique to Python. And this is actually usually done with a couple quotes although it works with either. And I can just put things on different lines, I can put spaces between things. And when I run this, it has it all formatted like that.
So all of this works because strings are first class objects in Python. Of course all of this applies as well to a string as a variable, and so if I simply assign a variable, and print, I can put the format on the variable. And it still works exactly as we expect. In fact, we can even sub-class strings in Python. So if I say class MyString, and put the original string class, the string class is called str. That's the built in string class in Python, and so I can sub-class from that.
And you'll recognize this if you watched my objects chapter. And I'll just take this out, and take out the format, and we just print S like this, and it'll print it backwards. Oh, I need to actually make it my string. And it'll print it backwards. So all of this makes strings extremely powerful in Python Three, and we'll get into more detail in the rest of this chapter.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.