Delving Into Strings
00:00
In the previous lesson, I showed you how literals behaved with __str__
and __repr__
. In this lesson, I’ll dive a little deeper into strings.
00:09
You’ve already seen that when you use print()
, it converts an object into a string and how under the covers that is calling the str()
callable, which under its covers is calling the __str__()
method.
00:21
There are other ways of turning something into a string in Python though. For example, there is the format()
method and f-strings. Both of these result in __str__()
getting called or most of the time anyway.
00:34 Let’s go see some examples in the REPL.
00:38 Once more in the REPL, and once more, I need an object.
00:49
Alright, there’s my datetime
object once more and its repr()
version. format()
can be called both as a method on a string object or as a built-in.
01:00 Let’s start with that first.
01:03
In this, its simplest form, format()
is the equivalent to using the str()
callable. This isn’t typically how you use the format()
call though.
01:12
Usually, you use the method attached to a string object to format the contents of the string. Let me try that. If you haven’t used the format()
mechanism, it looks for braces in the string and replaces them with the stringified version of the object passed into the format()
call as an argument. If I had multiple braces in the string, I would use multiple objects in the format()
call. Braces get replaced with the stringified version of today
, which of course is found using the str()
callable and our friend __str__()
.
01:45
With just braces alone, you might as well just use the str()
callable. format()
typically is used to jam strings into other strings through a templating fashion.
02:02
Odd sentence notwithstanding, now I’ve injected this stringified version of today
into the string template, replacing the curly brackets. If you’re newer to Python, you might not ever have used the format()
method as f-strings are pretty much superior in every way.
02:17
There’s actually even a third way of doing string formatting in Python, which resembles how it’s done in the C language and the format()
method was created to make it more readable than that, which was the original.
02:28
Nobody really liked the format()
method though, and eventually f-strings became the way to do it. F-strings tend to be easier to read as the template information also contains the variable names and they also ended up being faster than their format()
method equivalents.
02:44 So unless you’re using an unsupported version of Python, something old, there’s no reason not to use f-strings. Alright, all that about f-strings, let’s use one.
02:59
that’s the equivalent of that format()
method I just showed you. Like with the format()
method, the underlying conversion uses __str__()
. In the case of f-strings, it doesn’t have to though.
03:10
You can tell the f-string mechanism to use the repr()
version instead by using !r
, bang’s what us old mainframe folks call the exclamation point.
03:25
Now our template has the repr()
conversion injected into it. For this particular template, that doesn’t really make a lot of sense, but for debugging, this can be helpful.
03:35 Another trick you can use with an f-string is to append an equal sign after the variable.
03:46
This shows a keyword-value pair with the name of the variable and its repr()
contents. This is very handy when debugging as you get some insight into where your problem is and what it contains.
03:59
And just in case you wanted the keyword-value style, but with __str__()
instead, =!s
gets you there.
04:11 By modifying the contents of the brace brackets in an f-string, you can get any of the versions you like.
04:18 Next up, a little bit of a tangent about why I’ve been saying the word callable.
Become a Member to join the conversation.