many open books

Multiline strings in PHP and Python

Brevity may be the soul of wit but sometimes we need to wax loquacious. In other words, we occasionally need to work with longer strings. If you’re building a string up across a process then concatenation is your friend (or you can construct and then join a list). If you need a wall of text from the start, on the other hand, then you might consider a defining a single multiline string for the sake of convenience and legibility.

PHP Code Example:

Here, we construct a letter using a greeting and Lewis Caroll’s Jabberwocky.

$name = "Bob";

$poem = <<<JABBERWOCKY
Dear {$name},

'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.
JABBERWOCKY;

print $poem;

Python Code Example:

Here’s a functional equivalent in Python. It demonstrates both variable interpolation and the construction of a mutliline string.

name = "Bob"

poem = f"""
Dear {name},

'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.
"""

print(poem);

Discussion

PHP’s heredoc feature essentially supports a multiline double-quoted string. This means that variables will be interpolated within it as you’d expect. Python, on the other hand, does not require opening and closing identifiers for its multiline string support. Instead, you only need use three double quote characters to begin and end the the string.

Variables will not be interpolated by default but luckily all available string operations are supported in multiline mode. In this example, we use an f-string, and our poem/letter gets its recipient name.

Dear Bob,

'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.

Programmers are a fractious lot and opinions vary on the best way to manage multiple line strings. Personally, I prefer to deal with a single block of text when constructing short messages or long SQL queries. It is easier to manage basic formatting within a multiline string than across multiple joined fragments.

In tests and other situations where efficiency is not critical, you can use multiline strings as a convenient way of building lists out of cut and paste data. Simply copy the elements into a single string then split on the newlines to construct a list.

This multiline string can easily be turned into a list using the split() method.

quicklist = f"""
apples
pasta
bread
cookies
"""

lines = quicklist.split()
print(lines)

# output:
# ['apples', 'pasta', 'bread', 'cookies']

Of course, the obverse is also true. Another way of constructing a multiline string is to create a list and then join into a string when ready.

stringlist = [ 
    "hat",
    "gloves",
    "coat",
    "shoes"
]

print("\n".join(stringlist))

# output:
# hat
# gloves
# coat
# shoes

See also

Further Reading:


Stay up to date with the Python for PHP Programmers project


Yes! Please sign me up to this newsletter


Photo by Patrick Tomasso on Unsplash