Kotlin Program to Print a String



In this article, we will understand how to print a string. String is a datatype that contains one or more characters and is enclosed in double quotes(" ").

Below is a demonstration of the same

Suppose our input is ?

Hello my name is John!

The desired output would be

The string is: Hello my name is John!

Algorithm

  • Step 1 ? START

  • Step 2 ? Define a string variable

  • Step 3 ? Display the string on the console

  • Step 4 ? STOP

Example 1

In this example, we will print a string using the print() method in Kotlin. First, declare a string variable, which will be our input string ?

val inputStr = "Hello my name is David!"

Now, print the above string using the print() method ?

print("The string is defined as: " +inputStr)

Let us now see the complete example

fun main() { val inputStr = "Hello my name is David!" println("The string is defined as: " +inputStr) }

Output

The string is defined as: Hello my name is David!

Example 2

Here, we have created a custom function and printed a string

fun printStr(inputStr : String){ println("The string is defined as: " +inputStr) } fun main() { val inputStr = "Hello my name is John!" printStr(inputStr) }

Output

The string is defined as: Hello my name is John!
Updated on: 2022-10-13T12:22:15+05:30

578 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements