Strings
Rad has three delimiters for strings:
"double quotes"
'single quotes'
`backticks`
All three of these behave the same way. Rad offers three so you have alternatives to pick between depending on the contents of your string. For example, if you have a string which itself contains lots of single quotes, you may choose to use the double quotes delimiter. Or, if your string has both single and double quotes, you can use backticks to delimit your string. Specific example:
`Single quotes: 'Hi!', double quotes: "Hi!"`
String Attributes¶
- Not all strings are just plain text. They may have attributes such as color.
- This means that Rad contains logic on how to handle attributes when strings are combined or operated on
- e.g. concatenation, slicing, replace functions, etc
- The following operations maintain color attributes:
- concatenation
- index lookup
- The following do not, and just return a plain string:
- slicing (to be added)
- functions:
replace,split
- Attributes do not impact things like equality or comparing strings.
- A green string "Alice" and a yellow string "Alice" will be considered 'equal'.
String Interpolation¶
Formatting¶
- Float formatting does not require a
fat the end.- Correct:
{myFloat:.2} - Incorrect:
{myFloat:.2f}
- Correct:
Examples:
"{myString:20}"
"{myString:<20}"
"{myString:>20}"
"{myFloat:.2}"
Escaping¶
\will escape:{(to prevent string interpolation)\nnew line\ttab\i.e. itself, so you can write backslashes- The respective string delimiter itself, so
\",\', or\`, depending on the delimiter you're using.- However, it's advised you use delimiters that don't clash with the contents of your string, if possible.
Raw Strings¶
Raw strings can be used when you want Rad to treat the string as it's written, rather than performing escaping, interpolation, etc.
Raw strings are created by prefixing an r to the opening delimiter of your string. For example:
name = "alice"
text = r"Regards,\n{name}"
print(text)
Regards,\n{name}
Notice Rad did not render the \n as a newline as it would in a regular string,
and that {name} is also left as-is i.e. no interpolation was performed.
Unlike Python, you cannot escape anything in raw strings, including the delimiter. For example:
r"\""
is illegal because the backslash does not escape the following ", and so that quote actually ends the raw string.
Then we're left with a third and dangling " at the end, causing a syntax error.
You cannot escape the raw string's own delimiter
Rad raw strings behave more like their equivalent in Go than Python.
In Python, you can escape the delimiter used to make the raw string i.e. r"quote: \"!". If printed, this will
display as quote: \"! i.e. the escape character is also printed. There are lots of discussions online about this
somewhat odd behavior, which is why Rad (and Go) opted to instead keep the rules very simple and not allow escaping
in raw strings of any kind.
Instead, if you try the same thing in Rad, you will get an error because the quote following \ will close the
string, leaving a dangling !" at the end, which is invalid syntax.