Printing Output on Screen in Julia - GeeksforGeeks

28 Jul, 2021
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. 
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 integer4 # adding two integer4 + 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 int4 + 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
# expression2 + 2 - 4 * 5 + 12; # recall last evaluated expressionans
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 functionx = "The quick brown fox jumps over the lazy dog";print(x) # println functionx = "The quick brown fox jumps over the lazy dog";println(x)
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 functionshow("HELLO WORLD")
printstyled() function helps in printing out message in different colors.
Example: 
Julia
# using printstyled # printing text in different colorsfor color in [:red, :cyan, :blue, :magenta]    printstyled("Hello World $(color)\n"; color = color)end
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 Printfusing Printf # using printf macro with @ sign@printf("pi = %0.20f", float(pi))
And we can use @sprintf() function in order to create another string
Example: 
Julia
# importing Printfusing Printf # using @macro with sprintf where output is a string@sprintf("pi = %0.20f", float(pi))
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 functionopen("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 filereadline("geek.txt")
anikaseth98
sagar0719kumar
Picked
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Decision Making in Julia (if, if-else, Nested-if, if-elseif-else ladder)
Get array dimensions and size of a dimension in Julia - size() Method
Exception handling in Julia
Searching in Array for a given element in Julia
Find maximum element along with its index in Julia - findmax() Method
Get number of elements of array in Julia - length() Method
Join an array of strings into a single string in Julia - join() Method
Working with Excel Files in Julia
Getting last element of an array in Julia - last() Method
File Handling in Julia