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 withprintln("")outputs an empty line. - Length. Every string has a
lengthproperty that counts its characters, spaces included."I love coding!".lengthis14. - 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\\""printsQuote: "hi". An apostrophe needs no escape:"Let's code"is fine. - Special characters.
\\ninside a string is a newline, and\\tis 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"is55, not10. - Missing spaces.
"Hello" + "world"givesHelloworld. 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 and12is 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.