Strings¶
Strings represent text. Most of the time, they are used for debugging purposes.
1. Creating a string¶
Strings are created by enclosing text in double quotes:
Example of a string
2. String concatenation¶
2.1. Concatenating two strings¶
Strings can be concatenated using the +
operator:
Example of string concatenation
2.2. Concatenating a string and another value¶
A string can be concatenated with a value of any type. The value will be converted to a string.
Example of string concatenation with another value
It makes more sense when the value is a variable:
Example of string concatenation with a variable
3. Escape sequences¶
Certain characters cannot be used directly in a string. For example, since the double quote character is used to delimit strings, it cannot be used directly inside a string. To use such characters, you must use an escape sequence.
An escape sequence is a backslash (\
) followed by a character. The backslash
tells the game that the character that follows is special and should be treated
differently.
Here's a list of the most frequently used escape sequences:
Escape sequence | Character | Example | Result |
---|---|---|---|
\n |
Newline | "Hello\nworld!" |
Hello world! |
\t |
Tab | "Hello\tworld!" |
Cannot be shown in this table |
\" |
Double quote | "Hello \"world\"!" |
Hello "world"! |
\\ |
Backslash | "Hello \\world!" |
Hello \world! |