Language Reference: Printing and User Input

Printing values and getting user input in the console

For information on built-in functions for working with the console, see the standard library Console module.


Printing

The print function is used to display values.

print("Hello world!")
Hello world!

We can combine string interpolation with print to insert variable values into printed messages.

name = "Theo"
print("Hello $name!")
Hello Theo!

We can call print with values of any type. If print is called with a non-string value, it will display the string-representation of the value.

print(7 / 10)
print([])
print(sum)
0.7
[]
List.sum(numbers)

Prompting

The prompt function is used to get user input. prompt takes a string message which is displayed to the user before reading input.

name = prompt("Enter name: ")
"Hello $name!"
Enter name: Theo
"Hello Theo!"

The value returned by prompt will always be a string. You can use the Str.parse function to convert inputs to other primitive types.

f = parse(prompt("Enter degrees F: "))
c = (f - 32) / 9 * 5
"Degrees C: $c"
Enter degrees F: 77
"Degrees C: 25"