In addition to the rich set of string object methods, Python provides an assortment of functions for working with strings.
- [Instructor] The Python standard library provides a number of useful string-oriented functions beyond the methods of the string class itself. Here in Komodo, I open a working copy of hello.py from chapter 13 of the exercise files, and let's just go ahead and make this into a string. And I'm going to print the repr function of s, and when I run this, you notice that repr will print the string itself.
So, repr stands for representation. It'll print the best possible string representation of an object. What's interesting about this is if I create a class here, and let's say I initialize my variable with this. Now, when I print the representation of it, you'll notice it says, it's a bunny object at, and it gives me the memory address. This is not terribly helpful.
What representation does, what repr does is it will print the value returned by the special repr method. And so, if I say def_repr, and I'll return a string. And now, when I run this, the repr is going to have that string, the number of bunnies is 47.
Now is this distinct from what happens if I just print s like this, and I'm going to change this to x 'cause we're going to use a lot of different types here on all these strings. Now, when I run it, you'll notice that is says exactly the same thing. So the repr function will either print the return from the repr method of the class or, if it doesn't have that, it'll print this, we just change this, put an x in front of it, and when I run it, you'll see it prints this, main_.bunny object at.
On the other hand, if there's a string method this is the string representation, and I'm just going to put the word string in front of this, and this will be repr, so we can see which one is being preferred. Now, just the print function will take the string version, whereas the repr function will take the repr version. And so, this allows you to customize how it's going to be represented in different contexts.
So, if you do have the string version, it'll default to the string version. If you don't have the string version, it will default to the repr version. And if you don't have either of them, it will print that representation. Now, if we have the ascii function, works just like repr. And so, when I run this, you see, it uses the repr method, except it'll escape any special characters.
So, for example, if I put in an emoji of the live long and prosper hand, and I run this, you'll notice that it gives me this escaped value of the Unicode number. But if I use repr instead, it actually gives me the emoji. So that's the ascii function, which I'll make sure that that representation is only ASCII characters. There's also a character function, and I'm going to give it a number here, which I happen to know 'cause I have it written down, is the Unicode position for that love long and prosper hand.
And you'll notice that the character function, and I'm just going to print that, you'll notice that the chr function actually prints the character represented by that Unicode position. On the other hand, there's a ord function, and I'm just going to copy and paste our little Unicode character. And when I run that, oops, I need to put it in quotes, don't I? And when we run that you notice that we get that number.
So ord gives you the number of a character, and chr gives you the character of a number. So, Python provides a number of useful string-related functions. See phython.org for a complete reference here at docs.python.org/3/library/functions.html.
- 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