Use A Python Format String In A Variable
There are several ways to updates string variables with other variables. The two ways I use the most are [TODO: Code shorthand span ] and Template Strings.
(Note that this is for dealing with strings in variables. If I'm outputting a string directly I use [TODO: Code shorthand span ] which are detailed in the notes further below)
I use the [TODO: Code shorthand span ] option when there's not a lot of text (e.g. just one line) and the Template Strings when there's several lines (in which case I'm likely reading it in from a file.)
** Using .format()
This is the basic setup. A string is stored in the [TODO: Code shorthand span ] variable with [TODO: Code shorthand span ] acting as a placeholder for where the replacement value will be inserted.
The next line use calls [TODO: Code shorthand span ] with the single value we want to use to replace the [TODO: Code shorthand span ] placeholder.
printing it on the next line show our final ` hello, world ` output.
=
=
results start
You can use multiple [TODO: Code shorthand span ] placeholders as well. For example :
=
=
results start
** Template Strings
I use template strings for longer texts. They use "named placeholders" that start with a [TODO: Code shorthand span ] (e.g. ` $color ` , ` $fruit ` , and [TODO: Code shorthand span ] in this example)
Those values get filled in with their corresponding named arguments (e.g. ` color='blue' ` ) when the template is filled out with ` .substitute() `
For example :
=
=
results start
#+NOTES
This is what I use if I need to use a variable to hold the string that needs substitutions and replacements. Generally, I use that when I need to pass the string into a function, like :
=
return
=
=
results start
If I don't need to put the template for the string in a variable to pass it around I'll use an [TODO: Code shorthand span ] directly. For example :
=
=
results start