The Zeno Book Zeno 0.1 zeno-lang.org

6 Text

Text in Zeno is a str: a row of characters. It is written between double quotes, or between single quotes when it holds double ones:

let name = "Ada"
let quote = 'She said "hello"'
print(name, quote, "it's fine")
Ada She said "hello" it's fine

A string must close on the line it opens on.

6.1 Characters that cannot be typed

A backslash starts an escape: two characters that stand for one.

Escape Means
\n a line end
\t a tab
\r a carriage return
\", \' a quote
\\ a backslash
print("first line\nsecond line")
print("a backslash: \\, and a quote: \"")
first line
second line
a backslash: \, and a quote: "

Any other letter after a backslash is a mistake, so a slip such as "C:\temp\new" is caught rather than quietly turned into a tab and a line end.

6.2 Characters, not bytes

Zeno text is Unicode, kept in UTF-8. Its length, its positions and a loop over it all count characters, whatever alphabet they are from and however many bytes each one takes:

let word = "naïve"
print(len(word), word[2], word.find("v"))
5 ï 3

A position counts from 0, so word[0] is the first character. A position past the end, or below 0, is an IndexError:

print("abc"[3])
error: uncaught IndexError: index 3 is out of range for a str of length 3
 --> main.zeno:1:7
  |
1 | print("abc"[3])
  |       ^

In Python, text[-1] is the last character. That is handy, but it means a position that goes below zero by mistake quietly reads from the other end instead of failing. A clear error is the safer thing, above all in code that runs on hardware. (Decision 2.6.)

Text cannot be changed inside. Every method that seems to change it gives back new text and leaves the old as it was:

let word = "cat"
word[0] = "b"
error: an item cannot be set on str
 --> main.zeno:2:1
  |
2 | word[0] = "b"
  | ^

1 error found. Nothing was run.

6.3 Joining and comparing

+ joins two pieces of text. Both sides must be text; a number is made into text with str, or put into a formatted string:

let count = 3
print("count: " + str(count))
print("ab" == "ab", "ab" < "b", "B" < "a")
print("ell" in "hello", "x" not in "hello")
count: 3
true true true
true true

Text is ordered character by character, by the number Unicode gives each character, so every capital comes before every small letter. in asks whether one piece of text is found in another.

6.4 Formatted strings

An f in front of a string lets it hold expressions in braces. Each one is worked out and its value put into the text, as print would show it:

let name = "Ada"
let items = 3
print(f"{name} has {items} items, {items * 2} halves")
print(f"Shout it: {name.upper()}")
print(f"Braces themselves: {{ and }}")
Ada has 3 items, 6 halves
Shout it: ADA
Braces themselves: { and }

Inside the braces, a string must use the other kind of quote from the one around it.

Without the f, any string with a { in it would change meaning, and such strings are common: JSON, templates, messages for a device. The one letter marks the strings that hold code. (Decision 1.14.)

Formats

After the expression, a : and a format say how to lay the value out:

Format Means f"[{v:...}]"
.2f two digits after the point [3.14] for 3.14159
8.3f at least 8 wide, 3 digits after the point [ 3.142]
6 at least 6 wide; numbers on the right, text on the left [ 42]
<6, >6, ^6 at least 6 wide: left, right, centred [42 ]
03 at least 3 wide, with zeros in front [042]
x, X a whole number in hexadecimal [ff], [FF]
b a whole number in binary [101]
d a whole number [42]
let pi = 3.14159
let n = 42
print(f"[{pi:.2f}] [{pi:8.3f}] [{n:6}] [{n:<6}] [{n:^6}] [{n:04}]")
print(f"[{255:x}] [{255:X}] [{5:b}] [{0xA5:08b}]")
print(f"[{'ab':>5}] [{'ab':<5}]")
[3.14] [   3.142] [    42] [42    ] [  42  ] [0042]
[ff] [FF] [101] [10100101]
[   ab] [ab   ]

A format that makes no sense for the value is refused before the program runs:

let pi = 3.14159
print(f"{pi:x}")
error: `x` needs an int, but this is a float
 --> main.zeno:2:7
  |
2 | print(f"{pi:x}")
  |       ^

6.5 The methods of text

A method is a function that belongs to a value, and is called with a dot after it: name.upper(). Text has these:

Method Gives
upper(), lower() the text in capitals, or in small letters
strip() the text without blanks at either end
split() a list of the words, split at blanks
split(sep) a list of the pieces between each sep
lines() a list of the lines, without their line ends
replace(old, new) the text with every old made new
starts_with(part), ends_with(part) whether it starts or ends with part
find(part) the position where part starts, or none
slice(from, to) the characters from from up to, not including, to
slice(from) the characters from from to the end
code() the Unicode number of a text of one character
let line = "  Set Speed 40  "
let words = line.strip().lower().split()
print(words)
print("a,b,,c".split(","), "x=1".replace("=", " is "))
print("hello".starts_with("he"), "hello".ends_with("x"))
print("hello".slice(1, 4), "hello".slice(2))
print("A".code(), char(66), char(8364))
["set", "speed", "40"]
["a", "b", "", "c"] x is 1
true false
ell llo
65 B €

char(n) is the other way round from code(): the character with that number. It is a function and not a method because there is no text yet to ask: it makes text from a number, as str(n) does.

split and lines

split() with nothing in the brackets splits at any run of blanks and never gives an empty piece. split(sep) splits at each sep exactly, so two in a row give an empty piece between them:

print("one  two   three".split())
print("  a b ".split(" "))
print("".split(","), "a,".split(","))
["one", "two", "three"]
["", "", "a", "b", ""]
[""] ["a", ""]

lines() is the way to cut text into lines. It knows both kinds of line end, "\n" and "\r\n", and a last line with no end:

print("a\nb\n".split("\n"))
print("a\nb\n".lines())
print("one\r\ntwo".lines())
["a", "b", ""]
["a", "b"]
["one", "two"]

find gives a position or none

find gives the position of a piece of text, or none when it is not there. Because the answer may be none, Zeno will not let it be used as a number until the program has asked:

function first_word(line: str) -> str {
    let space = line.find(" ")
    if space is none {
        return line
    }
    return line.slice(0, space)
}

print(first_word("set speed 40"), first_word("stop"))
set stop

Python’s find gives -1 when the text is not there, and -1 is a number, so nothing stops it from being used as a position. none cannot be used as a position at all, so the mistake is caught before the program runs. (Decision 6.3.)

Values That May Be Missing is about none and how Zeno knows when it has been ruled out.

slice takes a part

slice(from, to) counts as range does: from from up to but not including to. A position outside the text is an error, not a shorter slice:

print("abc".slice(1, 9))
error: uncaught IndexError: slice 1 to 9 is out of range for a str of length 3
 --> main.zeno:1:7
  |
1 | print("abc".slice(1, 9))
  |       ^

If you write Python’s text[1:3], Zeno says what its own way is:

let text = "hello"
print(text[1:3])
error: a part of a list or a str is taken with `.slice(from, to)`, as in `xs.slice(1, 3)`
 --> main.zeno:2:13
  |
2 | print(text[1:3])
  |             ^

Plain letters only

upper and lower change the letters a to z and A to Z and nothing else, and strip and split() know the space, the tab, the line ends and the form feed as blanks. Letters of other alphabets pass through untouched:

print("Straße".upper(), "ÉCOLE".lower())
STRAßE École

The full rules for every alphabet need tables of tens of kilobytes, and a compiled Zeno program has to fit on a microcontroller. A method must also give the same answer under zeno run as in a compiled program, so the rule is the same everywhere. (Decision 3.14.)

6.6 A loop over text

A for loop over text is given each character in turn. With two names it is given each position and the character there:

for ch in "abc" {
    print(ch)
}
for at, ch in "hé" {
    print(at, ch)
}
a
b
c
0 h
1 é

Deciding and Repeating has loops in full.

6.7 Text from other values

str(x) gives any value as text, exactly as print shows it. Inside a list, text is shown in quotes, so that ["1"] and [1] look different:

print(str(42) + "!", str([1, "a"]), str(none), str(true))
42! [1, "a"] none true

A str is text, counted in characters. It is written in quotes, with escapes after a backslash, and never changes inside. + joins text, in finds it, and an f string puts values into it, laid out by a format. Its methods give new text, a list of pieces, a position or none.

  1. Write function last_word(line: str) -> str. There is no method that searches from the end. What is the shortest way with what there is?

  2. Print a table of the numbers 1 to 5 with their squares and cubes, each column 6 wide, using formats.

  3. Print "€".code() and work out by hand, from the number, how many bytes the character takes in UTF-8.

  4. What does "a,b,c".split(",") give? And ",".split(",")? Say why each piece is there.

  5. Write "C:\temp" in a program and read the message.