Mojo Output
Printing is how your program talks to you. You’ve used print since your first program, and it works a lot like Python’s. This page covers everything it can do: printing several values, changing what goes between and after them, special characters, printing errors to a separate stream, and a first look at t-strings for putting values inside text.
Printing a value
Section titled “Printing a value”print writes its value and then moves to a new line:
def main(): print("Hello, Mojo!") print(42) print(3.5) print(True)Output:
Hello, Mojo!423.5TrueText goes in quotes. Numbers and True/False don’t. You can use double quotes "..." or single quotes '...'; they mean the same thing.
Printing several values
Section titled “Printing several values”Give print several values separated by commas. It puts one space between them:
def main(): var name = "Ann" var age = 36 print("Name:", name, "Age:", age) print(1, 2, 3)Output:
Name: Ann Age: 361 2 3You can mix text and numbers freely here, because print handles each value on its own. That’s different from joining them with +, which only works between strings (more on that in Mojo String Formatting).
Printing lists and other collections
Section titled “Printing lists and other collections”print also understands lists, dictionaries and tuples:
def main(): var scores: List[Int] = [90, 75, 82] var names: List[String] = ["Ann", "Bo"] var ages: Dict[String, Int] = {"Ann": 31, "Bo": 25} print(scores) print(names) print(ages) print(len(scores))Output:
[90, 75, 82][Ann, Bo]{Ann: 31, Bo: 25}3Changing the separator with sep
Section titled “Changing the separator with sep”The space between values comes from the sep argument. Set it to anything you like:
def main(): print("2026", "11", "26", sep="-") print("a", "b", "c", sep=", ") print(1, 2, 3, sep="")Output:
2026-11-26a, b, c123sep="" means nothing at all between the values.
Changing the line ending with end
Section titled “Changing the line ending with end”After the last value, print writes a newline. That comes from the end argument, and you can change it too:
def main(): print("Loading", end="") print("...", end="") print(" done") print("A", end=" ") print("B", end=" ") print("C")Output:
Loading... doneA B CWith end="", the next print carries on on the same line. That’s handy when you print parts of a line in a loop:
def main(): for i in range(1, 6): print(i, end=" ") print() print("Finished")Output:
1 2 3 4 5FinishedAn empty print()
Section titled “An empty print()”print() with nothing inside prints just a newline. Use it to leave a blank line, or to end a line you built with end="" (like in the loop above):
def main(): print("Report") print() print("All good.")Output:
Report
All good.Special characters: escape sequences
Section titled “Special characters: escape sequences”Some characters are hard to type inside quotes, like a new line or a quote mark. You write them with a backslash and a letter. These are called escape sequences:
| Write | You get |
|---|---|
\n |
A new line |
\t |
A tab |
\" |
A double quote " |
\' |
A single quote ' |
\\ |
One backslash \ |
def main(): print("Line one\nLine two") print("Name\tAge") print("Ann\t36") print("She said \"hi\"") print("It\'s fine") print("C:\\Users\\ann")Output:
Line oneLine twoName AgeAnn 36She said "hi"It's fineC:\Users\annThe tab (\t) jumps to the next tab stop, so Name and Ann start at the same place, and so do Age and 36. The official literals reference lists a few more escapes, such as \u20AC for a Unicode character (€).
Raw strings: r"..."
Section titled “Raw strings: r"..."”Lots of backslashes get messy, for example in Windows paths. Put an r in front of the string and Mojo treats every backslash as a normal character:
def main(): print(r"C:\new\table") print("C:\new\table")Output:
C:\new\tableC:ew ableIn the second line, Mojo read \n as a new line and \t as a tab, which is not what you wanted. The raw string keeps the text exactly as written.
Text over several lines
Section titled “Text over several lines”For a longer block of text, use triple quotes. The line breaks you type are kept:
def main(): print("""Menu: 1. Start 2. Quit""")Output:
Menu: 1. Start 2. QuitPrinting errors to stderr
Section titled “Printing errors to stderr”Programs have two output streams. Normal output goes to stdout (standard output). Error messages and warnings go to stderr (standard error). Both show up in your terminal, but they can be sent to different places, so a log file of results doesn’t fill up with warnings.
To print to stderr, import it and pass file=stderr:
from std.sys import stderr
def main(): print("Result: 42") print("Warning: disk almost full", file=stderr)In the terminal you see both lines:
Result: 42Warning: disk almost fullNow send stdout to a file and watch what’s left:
mojo run report.mojo > results.txtOnly Warning: disk almost full appears in the terminal. Result: 42 went into results.txt. In Python you’d write print(..., file=sys.stderr); in Mojo it’s the same idea with from std.sys import stderr.
Showing output right away with flush=True
Section titled “Showing output right away with flush=True”Output is sometimes held back for a moment and written in bigger chunks, because that’s faster. Usually you don’t notice. But if you print a progress message without a newline and then do slow work, the message might not appear until later. flush=True says “write it out now”:
def main(): print("Working...", end="", flush=True) var total = 0 for i in range(1_000_000): total += i print(" done:", total)Output:
Working... done: 499999500000The output looks the same either way. The difference is when Working... shows up, which matters for long-running programs.
What print can’t print
Section titled “What print can’t print”print accepts any value whose type knows how to write itself as text. In Mojo, that means the type conforms to the Writable trait. Int, Float64, Bool, String, List and many others already do. None doesn’t:
def main(): print(None)hello.mojo:2:11: error: invalid call to 'print': an element of 'values' with type 'None' does not conform to trait 'Writable'; either prove the conformance with 'conforms_to', or add conformance print(None) ~~~~~ ^mojo: error: failed to parse the provided Mojo source moduleYou’ll see the same error if you try to print a struct you wrote yourself. Mojo Printing Your Types shows how to make your own types printable.
A first look at t-strings
Section titled “A first look at t-strings”Commas are fine for simple output, but sometimes you want values inside a sentence, without spaces around them. For that, Mojo has t-strings: put a t before the quotes and write expressions in {curly braces}:
def main(): var name = "Ann" var age = 36 print(t"{name} is {age} years old.") print(t"Next year: {age + 1}!")Output:
Ann is 36 years old.Next year: 37!If you know Python’s f-strings (f"..."), this will feel familiar, but Mojo uses t, not f. The print docs recommend t-strings for formatting output. The next lesson goes through them properly.
Practice
Section titled “Practice”1. A receipt
Section titled “1. A receipt”Print this receipt exactly. Use sep for the first line and a tab for the item lines.
Date: 2026-11-26Coffee 2.5Tea 2.0
Thank you!Hint: print("Date:", "2026-11-26") already gives you one space. For the date itself, think about sep="-". You need one empty print().
Show solution
def main(): print("Date: ", end="") print("2026", "11", "26", sep="-") print("Coffee\t2.5") print("Tea\t2.0") print() print("Thank you!")Output:
Date: 2026-11-26Coffee 2.5Tea 2.0
Thank you!end="" keeps the date on the same line as Date: , and sep="-" joins the three parts with dashes. \t adds the tabs, and the empty print() makes the blank line.
2. Quotes and paths
Section titled “2. Quotes and paths”Print these two lines exactly:
The file is "C:\data\new.txt"DoneHint: try it once with escapes, then once with a raw string for the path.
Show solution
def main(): print("The file is \"C:\\data\\new.txt\"") print("Done")Output:
The file is "C:\data\new.txt"DoneEach \" prints a quote and each \\ prints one backslash. Without the doubled backslashes, \n in \new.txt would become a new line. You could also print the path from a raw string: print("The file is", r'"C:\data\new.txt"').
print(a, b, c)prints values with a space between them and a newline at the end.sep=changes what goes between values;end=changes what goes at the end.print()on its own prints an empty line.- Escapes:
\nnew line,\ttab,\"quote,\\backslash.r"..."turns escapes off. file=stderr(fromstd.sys) sends errors and warnings to standard error.flush=Truemakes the text appear right away.printworks with anyWritabletype; t-strings liket"{name} is {age}"put values inside text.
Next up: Mojo String Formatting, building strings with t-strings, format() and friends.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- print (standard library): the official reference for
printand itssep,end,flushandfilearguments. - Mojo literals reference: string literals, every escape sequence, raw strings and t-strings.
- Writable trait: what a type needs so
printcan write it. - Mojo basics cheat sheet: a one-page summary including printing.