F-String
F-String is like sprintf()
, but more readable and concise. It provides a way to interpolate variables and expressions inside a string for formatting. Simply wrap the expressions in curly braces {}
, pass the string to f()
, and it will return the formatted string. These expressions can include variables, arithmetic operations, function calls, and more.
F-String is inspired by Python f-strings (formatted string literals).
While sprintf()
is handy for string formatting, it can be hard to write and read when there are many variables. F-String is simpler:
% sprintf()
sprintf("Product ID: %d, Name: %s, Description: %s, Price: %f, Quantity: %d, Date Added: %s, In Stock: %s", id, name, description, price, quantity, date_added, string(is_in_stock))
% f-string
f("Product ID: {id}, Name: {name}, Description: {description}, Price: {price}, Quantity: {quantity}, Date Added: {date_added}, In Stock: {is_in_stock}")
-
Variables: Variables can be directly inserted into f-strings using curly braces. (By default, they are converted to string with
string()
.)
>> name = "F-String";
>> f("Hello {name}!")
ans =
"Hello F-String!"
- Expressions: Arithmetic operations and function calls can be included within the curly braces.
>> x = 4;
>> f("{1 + sqrt(x)}")
ans =
"3"
- Arrays, Cell Arrays, Structs: They can also be included using curly braces.
>> myArray = [1,2,3];
>> myCellArray = {'a','b','c'};
>> myStruct.a = 10;
>> myStruct.b = 20;
>> f("{myArray(3)} {myCellArray{3}} {myStruct.a} {myStruct.('b')}")
ans =
"3 c 10 20"
-
Debugging: F-strings can simplify debugging by printing variable names and original expressions along with their values. Simply add
=
specifier after an expression.
>> device = 'Sensor1';
>> temperatureC = 25.5;
>> humidity = 70.0;
>> f('Readings {device=}, {temperatureC=}°C, {humidity=}%')
ans =
'Readings device=Sensor1, temperatureC=25.5°C, humidity=70%'
% Spaces in expression and around '=' are respected
>> f('{temperatureC * (9/5) + 32 = }°F')
ans =
'temperatureC * (9/5) + 32 = 77.9°F'
- Format specifier: F-strings support advanced formatting options such as specifying type, precision, and field width for the embedded expressions, using MATLAB's format specifier.
>> f('{12648430:%#X}') % hexadecimal format
ans =
'0XC0FFEE'
>> f('{0.123456789:%0.3f}') % precision
ans =
'0.123'
>> f('{123:%5d}') % field width
ans =
' 123'
>> f('{22 / 7 = :%0.3f}') % using both debugging and format specifier
ans =
'22 / 7 = 3.143'
-
Print Curly Braces: To print curly braces, escape them with double curly braces
{{ }}
.
>> f('text{{text}}text')
ans =
'text{text}text'
- Special Characters: To print special characters, use standard MATLAB escape sequence (see section "Text Before or After Formatting Operators").
>> f("1\t2\n3")
ans =
"1 2
3"
>> f('unicode heart: \x2665')
ans =
'unicode heart: ♥'
>> f('50%') % an exception is that '%' does not need to be escaped
ans =
'50%'
- Cell scalars can be embedded in expressions. They are converted to string with
string()
.
>> f("{ {{42}} }")
ans =
"42"
- Arrays cannot be embedded in expressions because they cannot be converted to string with
string()
.
>> f("myArray = {[1,2,3]}")
Error using [string](matlab:matlab.lang.internal.introspective.errorDocCallback('string'))
Conversion from cell failed. Element 1 must be convertible
to a string scalar.
- Nested f-string expressions will work as long as inner quotation marks are escaped the usual MATLAB way:
>> f('<a>{ f("<b>{ f(''c'') }</b>") }</a>')
ans =
'<a><b>c</b></a>'
-
See
FstringTest.m
for more use cases. -
See the
f.m
source code for how f-string works.
Download the source code from either MATLAB File Exchange or GitHub, unzip if needed, and place f.m
somewhere on your MATLAB path.
Thanks to these projects for inspiration:
- Python Formatted String Literals on API design
- MATLAB sprintf() on API design
- MATLAB-Language-grammar on how to parse single- and double-quoted strings
FString is open-sourced software licensed under the MIT license.
Cite As
Samuel Chan (2024). F-String (https://github.com/Sam-C/fstring/releases/tag/0.2.1), GitHub. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxTags
Acknowledgements
Inspired by: Regular expression based MATLAB Language grammar
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Version | Published | Release Notes | |
---|---|---|---|
0.2.1 | See release notes for this release on GitHub: https://github.com/Sam-C/fstring/releases/tag/0.2.1 |
||
0.2.0 | See release notes for this release on GitHub: https://github.com/Sam-C/fstring/releases/tag/0.2.0 |
||
0.1.0 |