Characters and strings
String basics
Characters & String Basics
We have been using strings since the very first lesson — every print("Hello World") contains one. 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 |