Skip to content

std.io

Console input and output: write lines to standard output and read lines from standard input. The functions here are free functions, so you import the ones you need by name.

from std.io import println, input

fun main() {
    let name = input("Your name: ")
    println("Hello, ${name}")
}

Importing

from std.io import print, println, input, read_line

std.io is a set of free functions, so use a selective import and list the names you want. A bare import std.io does not bring the functions into scope.

The global print builtin

You do not need std.io to print. The print builtin is always in scope and accepts any value whose type implements ToString, so it works on numbers, booleans, and your own types without conversion:

fun main() {
    print(42) // 42
    print(true) // true
    print(3.14) // 3.14
}

Several arguments print separated by spaces. As in Python, sep: changes the separator and end: replaces the trailing newline:

fun main() {
    print("a", 1, true) // a 1 true
    print(1, 2, 3, sep: ", ") // 1, 2, 3
    for i in range(3) {
        print(i, end: " ")
    }
    print() // 0 1 2
}

The print function from std.io is the str-only form below. The always-available builtin is the more convenient choice for most output; reach for std.io when you want println, input, or read_line.

Output

print(s: str)

Write s to standard output with no trailing newline. The argument must be a str; build one with interpolation or std.string methods if you have other values to include.

from std.io import print

fun main() {
    print("no newline here")
    print(" and more on the same line")
}

println(s: str)

Write s to standard output followed by a newline.

from std.io import println

fun main() {
    println("first line")
    println("second line")
}

eprint(s: str), eprintln(s: str)

Write to standard error, with no newline or with one. Standard error is not buffered, so the text shows at once, and it stays out of a pipe that reads standard output.

from std.io import eprintln

fun main() {
    eprintln("warning: no config found, using defaults")
}

Terminals

is_terminal() -> bool, stdin_is_terminal() -> bool

Whether standard output (or standard input) is attached to a terminal rather than a pipe or a file. Check it before emitting colors or prompting.

from std.io import is_terminal

fun main() {
    let warn = "\u{1b}[33mwarning\u{1b}[0m" if is_terminal() else "warning"
    print(warn)
}

Input

input(prompt: str) -> str

Write prompt to standard output with no trailing newline, then read one line from standard input and return it without its trailing newline. At end of input the returned string is empty.

from std.io import input

fun main() {
    let answer = input("Continue? (y/n) ")
    if answer == "y" {
        print("ok")
    }
}

read_line() -> str

Read one line from standard input and return it without its trailing newline, printing no prompt. At end of input the returned string is empty, which is how you detect that the stream is exhausted.

from std.io import read_line
import std.string

fun main() {
    let line = read_line()
    print(line.trim().to_upper())
}

Worked example: echo non-empty lines

Read lines until the input ends, skipping blank ones. The empty string from read_line at end of input ends the loop.

from std.io import read_line, println
import std.string

fun main() {
    var line = read_line()
    while not line.is_blank() {
        println(line.trim())
        line = read_line()
    }
}

See also

  • std.string for building and trimming the strings you read and print.
  • std.fmt for padding, joining, and number formatting before output.
  • std.fs when you want to read from and write to files instead of the console.