October 2018
Beginner to intermediate
466 pages
12h 2m
English
Brace characters are often useful in strings, aside from formatting. We need a way to escape them in situations where we want them to be displayed as themselves, rather than being replaced. This can be done by doubling the braces. For example, we can use Python to format a basic Java program:
classname = "MyClass"python_code = "print('hello world')"template = f"""public class {classname} {{ public static void main(String[] args) {{ System.out.println("{python_code}"); }}}}"""print(template)
Where we see the {{ or }} sequence in the template—that is, the braces enclosing the Java class and method definition—we know the f-string will replace them with single braces, rather than some argument in the surrounding methods. Here's ...