Taking Advantage of String Literal Concatenation
00:00 As it happens, you can do magic with Python and by magic I mean there’s a way to combine Python strings without using any operators. It’s called string literal concatenation.
00:10 I admit, maybe not the kind of magic that’ll get you a show in Vegas, but it’s still pretty cool. So what is string literal concatenation? When the Python interpreter encounters multiple string literals separated only by whitespace, they’ll be combined into a single string at compile time before your code even begins to run.
00:29
This is different from using the +
operator where strings are concatenated during runtime. It’s an interesting quirk to Python, and there’s a few common ways to leverage it in your code.
00:39 You can use it to combine different coding styles without having to use escape characters for the quote symbols. You can use it for splitting long strings conveniently across multiple lines.
00:50 And if you wanted to add comments to a specific part of a string, this is how you do it.
00:55 Now, as always, we go to the REPL to try it out.
00:59 First, let’s look at a short example. Write three strings with a space between each. The first string should read “Hello” with a comma, the second should consist of a single space, and the third should read “World!” with an exclamation mark at the end.
01:14 When you hit Enter, the result is the seamless concatenation of those three strings. `Hello, World!’ Python has combined the three strings together into one.
01:23 Of course, this is just an example. You wouldn’t want to write code like this. For a simple small string, there’s no need to use string literal concatenation.
01:32 A more motivating example would be a situation where you wish to write a string containing both apostrophes and double quotes, or to include code comments next to parts of the string.
01:43
Let’s do both. Create a variable called phrase
, and on the right hand of the assignment, your string will be on multiple lines, so to avoid an error, you’ll need to wrap it in parentheses.
01:54
Don’t worry, you’re still creating a string, not a tuple. phrase = (
, a string enclosed in double quotes, "Guido's talk wasn't named "
with a space at the end.
02:05 And a comment noting this is a string with apostrophes.
02:09
Next, on the line below, a string enclosed in single quotes with text inside enclosed in double quotes, "Feeding C
Python to ChatGPT"
.
02:21
And one more comment noting this is a string using double quotes inside, close parenthesis. Now if you print()
the phrase
, it comes out as one single sentence:
02:31
Guido's talk wasn't named
"Feeding CPython to ChatGPT"
. This example illustrates the two major use cases you might have for using string literal concatenation.
02:42 First, this is a clean way of commenting a string across multiple lines. Second, by defining the first string using double quotes and the second string using single quotes, you’re able to create one whole string that includes both single and double quotes.
02:57
This does, however, come with a trade-off. The code can easily be confused for an error like you meant to write a tuple and missed a comma. So for the sake of whoever may be reading your code, it could be preferable to instead use explicit string concatenation like using the +
operator ,or split the string using an escape character.
03:18 So when used judiciously, this can be a useful tool, but I do have to warn you, string literal concatenation can be a source of bugs as well. Here’s an example. Suppose you’re defining a list of strings, maybe your hobbies.
03:31
hobbies = [
and then four strings, "Reading"
, "Drawing"
, "Sculpting"
, and "Baking"
.
03:38
But do you notice something’s missing? The comma after the string "Sculpting"
. Since that was not intentional, we would like this to raise an error.
03:46
Unfortunately, if you examine hobbies
, you’ll see that instead your two hobbies have been combined. And though I’m pretty sure SculptingBaking
was the name of a game show I saw on the Food Network, it’s probably not what you intended to include in the list.
04:00 And because no error was raised, it might take you a while to figure out what exactly went wrong. So in general, because explicit is better than implicit, we recommend that you use the concatenation operator or its augmented form to combine long strings or strings with escape sequences.
04:17
And in the next lesson, you’ll see that sometimes you don’t even have to do the concatenating. You can let the print()
function handle it for you.
Become a Member to join the conversation.