print and println@printftry-catch for Exception Handlingfinallyprint and printlnOne of the most fundamental ways a program communicates is by displaying information. Whether it's showing the result of a calculation, providing a status update, or giving a simple greeting, sending output to the user's screen is an essential operation. Two primary functions handle this task for basic console output in Julia: print and println.
print Function: Output Without a New LineThe print function is your tool for sending data to the console. When you use print, Julia displays the values you provide, but it keeps the cursor on the same line after the output. This means if you call print multiple times in a row, the output from each call will appear side-by-side.
You can give print one or more items to display. These items can be direct values like text (strings) or numbers, or they can be variables that hold these values.
Let's look at how it works:
print("Hello, ") # Prints "Hello, " and the cursor stays on the same line
print("Julia") # Prints "Julia" immediately after the previous output
# Output: Hello, Julia
In this example, the first print call displays "Hello, ", and the second print call adds "Julia" right next to it, resulting in a single line of output: Hello, Julia.
You can also print different types of data. If you provide multiple arguments, print displays them one after another. If you need spaces, ensure they are part of your strings or provided as separate string arguments.
name = "World"
version = 1.0
print("Hello, ", name, "! We are using version ", version, ".") # Spaces are part of the strings
# Output: Hello! We are using version 1.0.
Here, print takes multiple arguments separated by commas, and it displays them consecutively on the same line.
println Function: Output With a New LineOften, you'll want each piece of output to appear on its own line. This is where println (short for "print line") comes in. The println function behaves very much like print, but with one significant difference: after displaying the specified values, it automatically adds a new line character. This moves the cursor to the beginning of the next line in the console.
Consider this example:
println("First line of output.")
println("This is the second line.")
# Output:
# First line of output.
# This is the second line.
Each call to println produces output on a new line, making the output easier to read in many situations.
Just like print, println can handle multiple arguments. These are printed consecutively, followed by a newline. Remember to include spaces within your strings or as separate arguments if needed.
count = 3
item = "apples"
println("I have ", count, " ", item, ".") # The " " ensures space between count and item
# Output: I have 3 apples.
Even with multiple arguments, println ensures the entire combined output for that call is followed by a new line.
print vs. println: A Quick ComparisonThe choice between print and println depends entirely on how you want to format your output. If you're building up a line of text piece by piece, print is useful. If you want distinct lines of output, println is generally preferred.
Let's see them side-by-side:
print("Status: ")
print("OK")
println() # Add a newline manually by calling println without arguments
println("Report:")
println("All systems nominal.")
# Output:
# Status: OK
# Report:
# All systems nominal.
In the first part, we used two print calls to construct a single line Status: OK. We then called println() without any arguments to simply move to the next line. The subsequent println calls each output on their own new line.
Both print and println are quite versatile and can display various types of data you'll encounter in Julia:
"Hello Julia".42), floating-point numbers (e.g., 3.14159).true and false.message = "Julia is cool!"
year = 2024
is_learning = true
println(message)
println("Current year: ", year)
println("Am I learning Julia? ", is_learning)
# Output:
# Julia is cool!
# Current year: 2024
# Am I learning Julia? true
Julia will automatically convert these basic types into a readable string representation for display. For more complex data structures like arrays or dictionaries, Julia often provides a sensible default output format as well, though you'll learn more about controlling their appearance later in your studies.
You've seen that print and println can accept multiple arguments. These arguments are printed one after another. If you want spaces between different items, you need to include those spaces explicitly, either as part of your strings or as separate string arguments (e.g., println(val1, " ", val2)).
fruit = "apple"
quantity = 5
println("Fruit: ", fruit, ", Quantity: ", quantity)
# Output: Fruit: apple, Quantity: 5
Here, the spaces and comma are part of the literal strings passed to println.
While passing multiple arguments works, a more common and often clearer method for constructing strings with embedded variable values in Julia is string interpolation. This technique allows you to insert the value of a variable (or the result of an expression) directly into a string using the $ symbol prefixing the variable or $(...) for an expression.
Let's revisit the previous example using string interpolation:
fruit = "apple"
quantity = 5
println("Fruit: $fruit, Quantity: $quantity")
# Output: Fruit: apple, Quantity: 5
This syntax is often more readable and convenient. You can also embed more complex expressions by wrapping them in parentheses after the $ sign:
radius = 6371 # Earth's radius in km
println("The Earth's approximate circumference is $(2 * pi * radius) km.")
# Output: The Earth's approximate circumference is 40030.17359196554 km.
String interpolation is a highly effective feature for creating formatted output strings in Julia.
Displaying output is a foundation of many programming tasks. Here are a few common scenarios where print and println are indispensable:
User Interaction: Providing prompts, messages, and feedback to the person running your program.
println("To the Area Calculator!")
print("Enter the radius: ") # User will type input after this prompt
Showing Results: Displaying the outcome of calculations or data processing.
a = 10
b = 20
sum_ab = a + b
println("The sum of $a and $b is $sum_ab.")
# Output: The sum of 10 and 20 is 30.
Simple Debugging: When you're trying to understand what your code is doing or why it's not working as expected, inserting println statements to show the values of variables at different points can be a quick way to diagnose issues.
x = 5
println("Initial value of x: $x")
x = x * 2
println("Value of x after doubling: $x")
# This helps trace how x changes.
While Julia has more sophisticated debugging tools, using println for quick checks is a common and often effective first step.
Effectively displaying output is fundamental for any interactive or informative program. Julia provides two straightforward functions for this:
print(...): Outputs the given arguments to the console without adding a new line at the end.println(...): Outputs the given arguments to the console and then moves the cursor to the next line.You can print various data types, and string interpolation with $ offers a convenient way to embed variable values directly within your output strings. These functions are your primary tools for communicating from your Julia program to the console.
Was this section helpful?
print and println.© 2026 ApX Machine LearningEngineered with