Characters and strings
We have been using strings since the very first lesson. Now let's look at strings properly and understand how they work under the hood.
What is a String?
A string is a sequence of characters. In Python, strings have the type str:
You can use either double quotes or single quotes — both are valid:
For multi-line strings, use triple quotes:
Special Characters and Escape Sequences
Some characters cannot be typed directly inside a string — like a newline or a tab. These are written using escape sequences: a backslash \ followed by a letter.
Output of the first two:
The most important escape sequences:
| Sequence | Meaning |
|---|---|
\n | Newline |
\t | Horizontal tab |
\\ | Literal backslash \ |
\" | Double quote (inside "...") |
\' | Single quote (inside '...') |
\r | Carriage return |
\0 | Null character |
In competitive programming, \n and are the only ones you will regularly encounter — mostly in input parsing and output formatting.