Formatted string literals#
Code Example
Runnable Example in Jac and JacLib
# Basic f-string examples
glob name = "John";
with entry {
print(f"Hello {name}");
print(F'Hello {name}');
print(f"{{It is myname}}, name is: {name}");
}
# Numeric operations and formatting
glob x = 10,
y = 20,
z = 65,
pi = 3.14159;
with entry {
print(f"Sum of {x} and {y} is {x + y}");
print(f"Hex: {y:x}");
print(f"Binary: {x:b}");
print(f"Value: {z:.2f}");
print(f"Hex: {z:10}");
print(f"Pi: {pi:.2f}");
}
# String formatting and conversions
glob b = "b",
name = "José",
value = "Hello\nWorld";
with entry {
print(f"Debug: {b!a}");
print(f"Default: {name}");
print(f"ASCII: {name!a}");
print(f"repr: {value!r}");
print(f"str: {value!s}");
print(f"ascii: {value!a}");
}
# Nested f-strings
glob name = "John";
with entry {
print(f"name is {name} {f'inner: {name}'}");
}
# Multiline f-strings with triple quotes
glob multiline_msg = f"""Hello {name},
This is a multiline f-string.
Your value is: {value}
Sum of {x} + {y} = {x + y}""";
with entry {
print(multiline_msg);
}
glob another_multiline = f'''Welcome {name}!
Here's your data:
- X: {x}
- Y: {y}
- Z: {z}
Binary of {x}: {x:b}''';
with entry {
print(another_multiline);
}
# Nested triple quote f-strings
glob nested_triple = f"""Outer: {name} {f'''Inner triple: {value}'''}""";
with entry {
print(nested_triple);
}
# Complex JSON-like formatting
glob complex_format = f"""
Debug Report for {name}:
{{
"x": {x},
"y": {y},
"hex_y": "{y:x}",
"repr_value": {value!r}
}}
""";
with entry {
print(complex_format);
}
# Raw f-strings for paths and patterns
glob path = "home",
file = "test.txt",
raw_path = rf"C:\Users\{name}\{path}\{file}",
raw_path2 = fr'D:\Projects\{name}\{file}';
with entry {
print(raw_path);
print(raw_path2);
}
# Multiline raw f-strings
glob raw_multiline = rf"""Path: C:\Users\{name}\Documents\
File: {file}
Full: C:\Users\{name}\Documents\{file}""";
with entry {
print(raw_multiline);
}
glob raw_multiline2 = fr'''Regex pattern: \d+\.\d+
Name: {name}
Pattern for {name}: \b{name}\b''';
with entry {
print(raw_multiline2);
}
# Raw f-strings with special patterns
glob regex_pattern = rf"\b{name}\w*\b",
raw_with_newline = rf"Line 1\nLine 2 with {name}\tTabbed",
windows_path = rf"\\server\share\{name}\documents\{file}";
with entry {
print(regex_pattern);
print(raw_with_newline);
print(windows_path);
}
# Basic raw f-strings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
Jac Grammar Snippet
Description
Formatted String Literals (f-strings)#
This document demonstrates the usage of formatted string literals (f-strings) in both Python and Jac languages. F-strings provide a concise and readable way to include expressions inside string literals.
Basic F-string Syntax#
F-strings are prefixed with f or F and use curly braces {} to embed expressions.
Python:
Jac:
Escaping Curly Braces#
To include literal curly braces in f-strings, double them:
Numeric Operations and Formatting#
F-strings support expressions and format specifiers for numbers:
Basic Operations#
Number Base Conversions#
- Hexadecimal:
{value:x}- converts to lowercase hex - Binary:
{value:b}- converts to binary - Decimal formatting:
{value:.2f}- formats to 2 decimal places
y = 20
x = 10
z = 65
pi = 3.14159
print(f"Hex: {y:x}") # Output: 14
print(f"Binary: {x:b}") # Output: 1010
print(f"Value: {z:.2f}") # Output: 65.00
print(f"Pi: {pi:.2f}") # Output: 3.14
Field Width#
String Formatting and Conversions#
F-strings support conversion flags to change how values are represented:
!r- callsrepr()on the value!s- callsstr()on the value!a- callsascii()on the value
b = "b"
name = "José"
value = "Hello\nWorld"
print(f"Debug: {b!a}") # ASCII representation
print(f"Default: {name}") # Default string representation
print(f"ASCII: {name!a}") # ASCII-safe representation
print(f"repr: {value!r}") # repr() representation with quotes
print(f"str: {value!s}") # str() representation
print(f"ascii: {value!a}") # ASCII representation with escapes
Nested F-strings#
F-strings can be nested inside other f-strings:
Multiline F-strings#
F-strings work with triple quotes for multiline strings:
Triple Double Quotes#
multiline_msg = f"""Hello {name},
This is a multiline f-string.
Your value is: {value}
Sum of {x} + {y} = {x + y}"""
Triple Single Quotes#
another_multiline = f'''Welcome {name}!
Here's your data:
- X: {x}
- Y: {y}
- Z: {z}
Binary of {x}: {x:b}'''
Nested Triple Quote F-strings#
Complex Formatting Examples#
F-strings are useful for generating structured output like JSON:
complex_format = f"""
Debug Report for {name}:
{{
"x": {x},
"y": {y},
"hex_y": "{y:x}",
"repr_value": {value!r}
}}
"""
Raw F-strings#
Raw f-strings combine the benefits of raw strings (no escape sequence processing) with f-string interpolation. They are prefixed with rf or fr.
Basic Raw F-strings#
path = "home"
file = "test.txt"
name = "John"
raw_path = rf"C:\Users\{name}\{path}\{file}"
raw_path2 = fr'D:\Projects\{name}\{file}'
Multiline Raw F-strings#
raw_multiline = rf"""Path: C:\Users\{name}\Documents\
File: {file}
Full: C:\Users\{name}\Documents\{file}"""
raw_multiline2 = fr'''Regex pattern: \d+\.\d+
Name: {name}
Pattern for {name}: \b{name}\b'''
Common Use Cases for Raw F-strings#
File Paths (Windows):
Regular Expressions:
Literal Backslashes:
Language Differences#
Both Python and Jac support the same f-string syntax with minor differences:
| Feature | Python | Jac |
|---|---|---|
| Statement termination | Optional | Required ; |
| Syntax | f"text {expr}" |
f"text {expr}"; |
| All formatting features | ||
| Raw f-strings | ||
| Nested f-strings |
Best Practices#
- Use f-strings for readability - They are more readable than
.format()or%formatting - Combine with format specifiers - Use
:notation for number formatting - Use raw f-strings for paths - Especially useful for Windows file paths and regex patterns
- Leverage conversion flags - Use
!r,!s,!afor debugging and special representations - Escape braces when needed - Double curly braces
{{}}for literal braces
Summary#
F-strings provide a powerful and intuitive way to format strings in both Python and Jac. They support:
- Variable interpolation
- Expression evaluation
- Number formatting and base conversion
- String conversion flags
- Multiline strings
- Raw string processing
- Nested f-strings
This makes them the preferred choice for most string formatting tasks due to their readability and performance.