Open In App

Printing Output on Screen in Julia

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Julia provides many methods of printing output on the screen. The Julia program starts with an interactive REPL (Read/ Evaluate /Print / Loop) as default. 

  • R: Reads what was typed;
  • E: Evaluates the typed expression;
  • P: Prints the return value;
  • L: Loops back and repeats it ;

It helps in outputting the result of the expression on the screen immediately. 

Print output using REPL

Using REPL, Julia provides the facility to evaluate the expression at the time of reading it, and further print the output of the expression. 

Example:

Julia




# x is a string
"hello"
 
# x is a integer
4
 
# adding two integer
4 + 4


If you don’t want the output to be printed you can use a semicolon(;) at the end.

Example: 

Julia




# example of ;
 
# addition of two int
4 + 4 ;
 
# concatenation of two strings
"hello" * "world";


To recall the last expression that was typed on the console we use ans command. The ans stores the last expression that was typed in the console.

Example:

Julia




# expression
2 + 2 - 4 * 5 + 12;
 
# recall last evaluated expression
ans


Print output using print() and println() function

The most common function to print the output of the program in the console of Julia is print() and println(). To execute this command we just need to press Enter on the keyboard. The main difference is that the println() function adds a new line to the end of the output.

Example: 

Julia




# print function
x = "The quick brown fox jumps over the lazy dog";
print(x)
 
# println function
x = "The quick brown fox jumps over the lazy dog";
println(x)


Print output using show() function

The show(io:: IO, x) function is also used to print output on the screen where the first argument is a stream. The REPL returns the output of show() function as a string.

Example:

Julia




# show function
show("HELLO WORLD")


Print output using printstyled() function

printstyled() function helps in printing out message in different colors.

Example: 

Julia




# using printstyled
 
# printing text in different colors
for color in [:red, :cyan, :blue, :magenta]
    printstyled("Hello World $(color)\n"; color = color)
end


Print output using printf() function

Julia also supports printf() function which is used in C language to print output on the console. Julia macro (which is used by prefacing it with the @ sign) provides the Printf package which needs to be imported in order to use. printf() is also used as a formatting tool. Here %f is used for conversion.

Example: 

Julia




# importing Printf
using Printf
 
# using printf macro with @ sign
@printf("pi = %0.20f", float(pi))


Print output using sprintf() function

And we can use @sprintf() function in order to create another string

Example: 

Julia




# importing Printf
using Printf
 
# using @macro with sprintf where output is a string
@sprintf("pi = %0.20f", float(pi))


Print output using write() function

write() function is used to write the output to a file and it prints the no. of characters present in the string on the console.

Example: 

Julia




# write function
open("geek.txt", "w") do io
     write(io, "The quick brown fox jumps over the lazy dog");
end
 
# the readline() function to show the output of the file
readline("geek.txt")




Last Updated : 28 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads