Strings¶
Strings in Kod are UTF-8 byte sequences. The type is str.
Literals¶
Standard escape sequences are supported: \n, \t, \\.
F-strings¶
Interpolate expressions into strings with f"...":
let name: str = "world"
print(f"hello, {name}!") // hello, world!
let n: int64 = 42
print(f"the answer is {n}") // the answer is 42
Anything inside { } is a Kod expression. Non-str values are auto-converted via their to_str method:
print(f"{a} + {b} = {a + b}")
print(f"flag={true}") // flag=true
print(f"items: {items.len()}") // any type with to_str works
Concatenation¶
Use + to concatenate strings, or += to append in place:
let greeting: str = "hello" + ", " + name + "!"
let buf: str = "lines:\n"
buf += " one\n"
buf += " two\n"
+= works on struct fields too: obj.message += " (cont.)".
Multi-line strings¶
Triple-quoted strings span multiple lines:
The closing """ must sit on its own line. Its indent anchors the
dedent — every content line has at least that many leading spaces (or
tabs) stripped. The newline immediately after the opening """ is
not part of the value; the newline before the closing """ is. So
the example above produces:
(four-space indent gone; trailing newline kept).
Combine with f for interpolation:
Escape sequences (\n, \t, \\) work the same as regular strings.
Each content line must be indented at least as far as the closing
"""; a content line with less indent is a lex error.
Length¶
Character access¶
Index into a string to get the ASCII value of a character (as int64):
Negative indices count from the end:
Slicing¶
Either endpoint may be omitted: s[..k] is s[0..k] and s[k..] is s[k..len(s)]. s[..] returns a copy.
Comparison¶
String equality compares contents, not identity.
Conversion¶
Primitives have a to_str method:
let s: str = 42.to_str() // "42"
let t: str = true.to_str() // "true"
let u: str = "hi".to_str() // identity
Structs can opt in by declaring func to_str(self) -> str. F-string interpolation calls to_str automatically.
See Built-ins for the full list of string-related functions.