Start learning free

LESSON 2 OF 348 · WELCOME TO KOTLIN

Introduction to Strings in Kotlin: Text in Double Quotes

Lesson Read the full explanation

TL ; DR:

  • To create a string in Kotlin, we put some text inside double quotes ("Hello world").


  • Be careful not to forget any quote! This code missing a " at the end:

    println("Welcome!)

    would throw a SyntaxError



Full lesson:

How boring would our life as humans be if we communicated only using numbers? Luckily we have letters, words and languages to express what we think!

We can also use letters and words in Kotlin to express more meaningful messages like we do in real life.


Strings:

In programming, a string is any block of text e.g. any sequence of characters from your keyboard (letters, numbers, spaces, symbols, etc.).

To create a string in Kotlin, we put some text inside double quotes like this "Hello world".

For example, we can use strings inside the println() function for printing messages to the console:

println("My name is Andy")
println("Welcome to AlgoCademy!")

The output of this code is:

My name is Andy
Welcome to AlgoCademy!


As you can see, the quotes are not printed. That's because quotes are not part of the string. Their job is solely to let Kotlin know when a string declaration starts and when it ends.


Assignment
Follow the Coding Tutorial and let's work with strings!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · WELCOME TO KOTLIN

    Introduction To Strings in Kotlin, explained

    What a string is

    Programs work with text as well as numbers: names, messages, file contents, anything made of characters. In programming, a string is a sequence of characters: letters, digits, spaces, punctuation and symbols. In Kotlin, you create a string by putting the text between double quotes:

    "Hello world"
    

    Written like this, "Hello world" is a string literal: a piece of text written directly in the code. Its type is String.

    Printing strings

    A string can go anywhere a value can. The most common first use is printing it with println:

    println("My name is Andy")
    println("Welcome to AlgoCademy!")
    

    The output is:

    My name is Andy
    Welcome to AlgoCademy!
    

    The quotes don't appear in the output. They aren't part of the string. Their only job is to tell Kotlin where the text starts and where it ends.

    Strings and numbers are different

    The string "12" and the number 12 look alike when printed, but they behave differently. Numbers support arithmetic, and strings are just characters. Joining two strings with + glues them together:

    println(12 + 1)
    println("12" + "1")
    println("ab" + "cd")
    
    13
    121
    abcd
    

    A string with digits in it is still text. Kotlin doesn't calculate with it.

    Useful things to know

    • The empty string. "" is a valid string with no characters. Printing it with println("") outputs an empty line.
    • Length. Every string has a length property that counts its characters, spaces included. "I love coding!".length is 14.
    • Single and double quotes are different. In Kotlin, single quotes make a Char, exactly one character, such as 'x'. Text is always in double quotes. 'hello' doesn't compile.
    • Quotes inside a string. To put a double quote inside a string, write a backslash before it: "Quote: \\"hi\\"" prints Quote: "hi". An apostrophe needs no escape: "Let's code" is fine.
    • Special characters. \\n inside a string is a newline, and \\t is a tab.
    • Strings are immutable. No operation changes a string in place. Operations such as "Ab".uppercase() give back a new string, "AB".

    Reading characters and inserting values

    Square brackets read one character of a string by its position. Positions start at 0, so the first character is at position 0. The result is a Char:

    println("hello"[0])
    
    h
    

    A $ followed by a variable's name inside a string inserts that variable's value into the text. With val name = "Andy", the string "Hi, $name!" is Hi, Andy!. To print a real dollar sign, write \$. These features come up again once you know variables, so for now it is enough to know that a string is more than fixed text.

    Common mistakes

    The compiler reads the quotes literally, so a missing one confuses it. If you forget the closing quote:

    println("Welcome!)
    

    Kotlin treats everything after the opening quote as part of the string, so the string never ends, and the compiler reports errors such as syntax error: Expecting '"'. and syntax error: Expecting ')'. Nothing runs until the code compiles.

    Using single quotes for text is another common slip:

    println('hello')
    

    This gives too many characters in a character literal, because single quotes only hold one character.

    Other things to watch for:

    • Mismatched quotes. A string that opens with one kind of quote and closes with another never ends. Open and close with ".
    • Quotes around a number you want to calculate with. "5" + "5" is 55, not 10.
    • Missing spaces. "Hello" + "world" gives Helloworld. Spaces are characters, and Kotlin doesn't add them for you.

    The exercise

    The lesson asks you to print the string I love coding!. There is one step: call println and put the text between double quotes, spelled exactly as asked, including the capital letter and the exclamation mark.

    println("I love coding!")
    

    The console shows I love coding!, without the quotes.

    Key takeaways

    • A string is text, written between double quotes.
    • The quotes mark where the string starts and ends. They aren't printed.
    • "12" is text and 12 is a number, and they don't behave alike.
    • Single quotes are for a single Char, not for text.
    • A missing quote is a compile error, so check that every string has both.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free