Skip to content

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.

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!
42
3.5
True

Text goes in quotes. Numbers and True/False don’t. You can use double quotes "..." or single quotes '...'; they mean the same thing.

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: 36
1 2 3

You 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).

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}
3

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-26
a, b, c
123

sep="" means nothing at all between the values.

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... done
A B C

With 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 5
Finished

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.

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 one
Line two
Name Age
Ann 36
She said "hi"
It's fine
C:\Users\ann

The 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 (€).

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\table
C:
ew able

In 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.

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. Quit

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: 42
Warning: disk almost full

Now send stdout to a file and watch what’s left:

Terminal window
mojo run report.mojo > results.txt

Only 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.

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: 499999500000

The output looks the same either way. The difference is when Working... shows up, which matters for long-running programs.

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 module

You’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.

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.

Print this receipt exactly. Use sep for the first line and a tab for the item lines.

Date: 2026-11-26
Coffee 2.5
Tea 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-26
Coffee 2.5
Tea 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.

Print these two lines exactly:

The file is "C:\data\new.txt"
Done

Hint: 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"
Done

Each \" 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: \n new line, \t tab, \" quote, \\ backslash. r"..." turns escapes off.
  • file=stderr (from std.sys) sends errors and warnings to standard error.
  • flush=True makes the text appear right away.
  • print works with any Writable type; t-strings like t"{name} is {age}" put values inside text.

Next up: Mojo String Formatting, building strings with t-strings, format() and friends.

Quick check

1. What does print("a", "b", "c", sep="-") print?
2. You print a List[String] holding "Ann" and "Bo". What do you see?
3. How do you print a warning to standard error?
4. What does print(r"a\tb") print?