You have already learned how to move a string or glue several ones together to get a new expression. But there are alternatives to these operations in programming. They make the code more readable and easier to maintain.
Interpolation
The most common way to join strings is concatenation. Using it, we add them to each other, as in the example below:
first_name = 'Joffrey'
greeting = 'Hello'
print(greeting + ", " + first_name + "!")
# => Hello, Joffrey!
Concatenation doesn't always look so visually clear. The quotation marks make it difficult to see what the result will be. And the more complex the string is, the more confusing it looks. Concatenation has an alternative — interpolation. It is what it looks like:
first_name = 'Joffrey'
greeting = 'Hello'
print(f'{greeting}, {first_name}!')
# => Hello, Joffrey!
The letter f
indicates that we're creating an f-string, a pattern into which variable values are substituted using curly brackets. The output is a regular string.
Consider this example:
school = 'Hexlet'
what_is_it = f'{school} - online courses'
print(what_is_it) # => Hexlet - online courses
In almost all languages, interpolation is preferable to concatenation for combining strings. It sticks them together, with the spaces and characters visible inside them.
Interpolation helps make code clearer to developers. But that's not the only alternative we want to talk about. Next, let's figure out how to declare a multi-line string and not use \n
.
Multi-line strings
Imagine we need to define a string that consists of several lines, i.e., we have the line feed \n
inside. For example, it'll look like this:
text = 'Sample text that,\consists of\more than one line'
The string will look very different when printed:
Sample text,
consisting of
a few lines
For such situations, Python has another way of creating strings called multi-line strings. To define a multi-line string, you need to enclose it in triple quotes - """
or '''
. Using a multi-line string, you can carry the text and not use a line break \n
:
text = '''Sample text,
consisting of
a few lines
'''
Sample text,
consisting of
a few lines
Note that there is a blank line at the end of the text. It appeared in the text because we put the closing quotation marks '''
on the new line. If you don't move the closing quotation marks to a new line, there'll be no empty line in the text:
text = '''Sample text,
consisting of
a few lines.'''
Sample text,
consisting of
a few lines
Because of the triple quotes, multi-line strings mean you don't have to escape quotes within a string:
There's no need to escape the 'single' and "double" quotes
Even multi-line strings can become f-strings for interpolation purposes:
a = 'A'
b = 'B'
# We added the letter `f`` on the left
text = f'''{a} and {b}
sitting in a tree
'''
A and B
sitting in a tree
The computer doesn't recognize what connection and line spacing methods you use. It'll still do the calculations and give you the result you want. We use interpolation and multi-line strings to make it easier for developers to read code.