Standard output is the basic operation that displays information on a device. Not every program generates such output. By default, the standard output displays the data on the screen, but it is possible to redirect it to a file.
On Hyperskill, you will often write programs that send data, for example, strings and numbers, to the standard output.
Printing text
Kotlin has two functions that send data to the standard output: println and print.
The println function (print line) displays a string followed by a new line on the screen. For example, the code snippet below prints four lines:
println("I")
println("know")
println("Kotlin")
println("well.")
Output:
I
know
Kotlin
well.
As you can see, all strings are printed without double quotes.
You can also print an empty line:
println("Kotlin is a modern programming language.")
println() // prints an empty line
println("It is used all over the world!")
Output:
Kotlin is a modern programming language.
It is used all over the world!
The print function displays a value and places the cursor after. Let's look at the example below. This piece of code outputs all strings in a single line:
print("I ")
print("know ")
print("Kotlin ")
print("well.")
Output:
I know Kotlin well.Printing numbers and characters
With println and print functions, a program can print not only strings but also numbers and characters.
Let's print two secret codes:
print(108) // prints a number
print('c') // prints a character
print("Q") // prints a string
println('3') // prints a character that represents a digit
print(22)
print('E')
print(8)
println('1')
Output:
108cQ3
22E81
Just like with strings, there are no quotes.
The $ operator
In the Kotlin programming language, the $ operator is often used in string templates to insert the values of variables or expressions directly into a string.
Examples of use:
Inserting the value of a variable:
val name = "Alice"
println("Hello, $name!") // Output: Hello, Alice!
Inserting the value of an expression:
val a = 5
val b = 10
println("Sum of $a and $b is ${a + b}") // Output: Sum of 5 and 10 is 15
Note that when you want to insert a more complex expression or access object properties, you use curly braces {} around the expression.
Well, you may guess, how to print $ in Kotlin. Here some ways to do it:
val a = 20
// There is a dot after $, not variable, so no problem here
println("The price is $a$.") // Output: The price is 20$.
// The second $ insert the variable and the first a common symbol here
println("The price is $$a.") // Output: The price is $20.
// \$ means that you take $ and don't mind about special interpretation
println("The price is \$a.") // Output: The price is $a.
//Another example with \$
println("The price is \$$a.") // Output: The price is $20.
Thus, the $ operator in Kotlin allows for the convenient and quick insertion of variable values and expression results directly into string literals.
Conclusion
In this topic, we have covered two basic functions to redirect information to the standard output: println and print. println prints information followed by a new line, while print outputs them one after another. Remember, we can use these functions to output not only strings but also any other characters as well.