Skip to Content
Read

Strings and output

Str and Char, building text, interpolation and print options.

Str is a sequence of ASCII bytes indexed by 0..s.len; Char is one byte. There is no Unicode decoding behind a subscript, so s[i] is O(1) and is exactly the i-th character of the input.

Reading and indexing

read() into a Str reads one whitespace-separated token.

Output
Press Run (Ctrl+Enter).

A character minus 'a' is its position in the alphabet, and 'a' + k goes back:

Output
Press Run (Ctrl+Enter).

Building text

A var string is a buffer. push appends a character, += appends a string, and "{...}" puts any value into text:

Output
Press Run (Ctrl+Enter).

Integers have no .to_str(); write "{n}".

print

print puts a space between its arguments and ends the line. Its named arguments cover the usual output shapes:

Output
Press Run (Ctrl+Enter).
  • A list or array prints on one line, separated by spaces.
  • sep: and end: change the separators; none: says how to print a missing value.
  • yes(b) prints YES or NO; print does not take a bare Bool.
  • Format specs follow Python: :.2f, :03.

Output is buffered and flushed at the end, so printing 2·10⁵ lines is fast without any setup.

Problem: Run length

Replace each maximal run of equal letters in a word by the letter and the run's length: aaabcc becomes a3b1c2.

5 tests · Ctrl+Enter
Hints

out.push(c) and out += "{len}" build the answer in one pass.

More like this: Mirror word. Next: Sorting and searching.

Lints for these patterns

cpl lint, the editor and the playground flag the hand-written versions of what this chapter teaches:

On this page