Print functions in Scala

Print,Println,Printf

Go to Spark-shell or choose environment of your choice to run scala code.
Write below code :

 print("hello data engineers")

If you type print and press the tab ,you can see three functions ,Related to print ,these are print,printf and println. 

like above line, it just print whatever is written in the bracket. but it is generally used only when you need to get single output.

See below example in which , print statements are written in one line and three times. and output is messed up . in that scenario , you can use prinltn function which prints the output and new line. generally in loop it is used ,so for each iteration you can have output in new line.

 print("hello data engineers"); print("hello again");print("hello once again")

use below for new line

 println("hello data engineers"); println("hello again");println("hello once again")

 

Now Printf , it used for format string.

in below code , the format string is “my name is %s” where %s can be replaced by string in next parameters.

 printf("my name is %s","bigdataprogrammers.com")

Another example 

 printf("id is %d",)

Some of your Questions are answered here :

1.  Can I use variable in print ?

Yes you can : use below code

var name="ashish"
var age="27"
println(raw"my name is $name and age is $age")

2. Can I use concatenation in print ?

Yes you can : use below code 

 println("my name is "+name)

Hope you like this tutorial. share in your network for wider reach. and don’t forget to subscribe us for upcoming tutorials.

If you want to see this tutorial in action , visit our you tube channel.

Sharing is caring!

Subscribe to our newsletter
Loading

Leave a Reply