- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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!
Advertisements
To Continue Learning Please Login
Login with Google