Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I interpolate variables in Python heredocs?

In Perl languages, I can interpolate in double quoted heredocs:

Perl:

#!/bin/env perl
use strict;
use warnings;

my $job  = 'foo';
my $cpus = 3;

my $heredoc = <<"END";
#SBATCH job $job
#SBATCH cpus-per-task $cpus
END

print $heredoc;

Raku (F.K.A. Perl 6):

#!/bin/env perl6

my $job  = 'foo';
my $cpus = 3;

my $heredoc = qq:to/END/;
    #SBATCH job $job
    #SBATCH cpus-per-task $cpus
    END

print $heredoc;

How do I do something similar in Python? In searching "heredoc string interpolation Python", I did come across information on Python f-strings, which facilitate string interpolation (for Python 3.6 and later).

Python 3.6+ with f-strings:

#!/bin/env python3

job  = 'foo'
cpus = 3
print(f"#SBATCH job {job}")
print(f"#SBATCH cpus-per-task {cpus}")

All three of the above produce the exact same output:

#SBATCH job cutadapt
#SBATCH cpus-per-task 3

That's all nice and everything, but I'm still really interested in interpolation in heredocs using Python.

like image 322
Christopher Bottoms Avatar asked Apr 17 '18 19:04

Christopher Bottoms


People also ask

Does Python have string interpolation?

Python 3.6 added new string interpolation method called literal string interpolation and introduced a new literal prefix f . This new way of formatting strings is powerful and easy to use. It provides access to embedded Python expressions inside string constants.

How is string interpolation performed in Python?

String interpolation is a process of injecting value into a placeholder (a placeholder is nothing but a variable to which you can assign data/value later) in a string literal. It helps in dynamically formatting the output in a fancier way. Python supports multiple ways to format string literals.

What is '$' in Python?

$$ is an escape; it is replaced with a single $. $identifier names a substitution placeholder matching a mapping key of "identifier" . By default, "identifier" must spell a Python identifier. The first non-identifier character after the $ character terminates this placeholder specification.

What is heredoc in Python?

In computing, a here document (here-document, here-text, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file.

What is interpolation syntax?

The interpolation syntax is powerful and allows you to reference variables, attributes of resources, call functions, etc. You can also perform simple math in interpolations, allowing you to write expressions such as ${count. index + 1} .


1 Answers

Just for the record, the other string formatting options in Python also work for multi-line tripple-quoted strings:

a = 42
b = 23

s1 = """
some {} foo
with {}
""".format(a, b)

print(s1)

s2 = """
some %s foo
with %s
""" % (a, b)

print(s2)
like image 200
moritz Avatar answered Sep 30 '22 08:09

moritz