Skip to content
Related Articles

Related Articles

Printing Output on Screen in Julia
  • Last Updated : 25 Aug, 2020

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



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Example:

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Example:

Julia



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# show function
show("HELLO WORLD")

chevron_right


Print output using printstyled() function

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

Example:

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Print output using sprintf() function

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

Example:

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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")

chevron_right


My Personal Notes arrow_drop_up
Recommended Articles
Page :