Python Programming · Foundations
Strings
On this page 9 sections
In 30 seconds
A Python string An ordered, immutable sequence of text characters in Python. Full entry → is text written as a sequence of characters, usually between quotes. You can read one character with an index An integer position used to select one item from a sequence. Full entry →, take a range with a slice A selection of a sequence between boundaries, written with colon notation such as text[1:4]. Full entry →, and join strings with +. Strings are immutable Unable to have its existing contents altered in place. Full entry →: an operation that appears to change text instead creates a new string. F-strings let a literal include the value of an expression inside braces.
Why this matters
Programs constantly work with names, labels, messages, and other text. Strings give that text predictable rules: positions start at zero, slices stop before their ending boundary, and the original text cannot be edited in place. Those rules make it easier to diagnose an off-by-one result, assemble a clear message, or understand why an attempted character assignment fails. Careful string reasoning also carries into later work with lists, files, and user-facing output.
The college version
Text values and literal notation
A string is Python's built-in representation for textual data. A program usually creates one with a quoted literal, such as 'hello' or "hello". Choosing one quote style over the other can make punctuation easier to express: "don't" needs no backslash around its apostrophe, while 'She said "yes"' needs no backslash around the double quotes. The quote marks are source notation; they delimit the value rather than becoming part of it. A string can be empty ('') and can contain one character. Python does not introduce a separate character type for that one-character case: 'P' is a string whose length is one.
Backslash begins an escape sequence A backslash-led notation in a literal that represents a character or special action, such as \n for a newline. Full entry → inside an ordinary string literal Source-code notation that creates a string value, usually by placing text between matching quotes. Full entry →. For example, '\n' represents a newline, '\t' represents a tab, and '\' represents one backslash. This lets source code describe characters that would otherwise be awkward to place directly in a literal. Python also recognizes Unicode escape forms; for example, '\N{GREEK SMALL LETTER PI}' evaluates to π. This lesson concerns the text value after Python evaluates a literal, not the encoding of a source file or the mechanics of reading text from a file.
A string is an immutable sequence
Strings are sequences: their characters have an order and positions. The first character has index 0, so in word = 'Python', word[0] is 'P'. Negative indices count back from the end: word[-1] is 'n'. Asking for one position with an index returns a one-character string. An index outside the available positions raises IndexError, which is a useful signal that a calculation selected no character.
A slice uses a colon to select a run of positions. In word[1:4], the start position 1 is included and the stop position 4 is excluded, producing 'yth'. Omitting the start means the beginning; omitting the stop means the end. Thus word[:2] is 'Py' and word[4:] is 'on'. Thinking of a slice as boundaries between characters prevents a common off-by-one error: the right boundary is not included. Unlike a single out-of-range index, a slice can extend beyond the available boundaries and is clipped by the sequence rules.
Immutable means the characters of an existing string cannot be replaced in place. word[0] = 'J' raises TypeError. To get different text, calculate a new string, perhaps with a slice and concatenation Creating one sequence by joining sequences end to end. Full entry →. This does not make strings unusable; it makes their existing character sequence stable. Do not confuse immutability with a rule that a program can never use a different value later. A name may be bound to a new string, but that is distinct from altering characters within an already existing string.
Building text with operations and f-strings
The + operator concatenates strings: 'Ada' + ' Lovelace' produces 'Ada Lovelace'. The space belongs in the second literal; concatenation does not automatically insert punctuation or spaces. This is a direct way to assemble a small fixed message. It is also a place to inspect types carefully: + joins two strings here, while adding numbers is covered separately.
An f-string A formatted string literal whose replacement fields in braces contain expressions. Full entry → is a string literal with an f prefix, such as f'{name} has {items} tasks.'. Braces introduce replacement fields containing expressions. When Python evaluates the f-string, it evaluates each expression and incorporates its result into the resulting text. With name = 'Ada' and items = 3, that example produces 'Ada has 3 tasks.'. The prefix is what makes braces special in this form; ordinary string literals do not evaluate text between braces. Use f-strings when a message has fixed surrounding wording and a few values to insert.
A disciplined trace follows each operation. For 'Python'[1:4], mark positions 1, 2, and 3, then stop before 4: y, t, h. For f'{2 + 3} items', calculate the expression first, then place 5 into the sentence. These small traces establish a reliable habit before later lessons add methods, input, or more complex data structures.

Eli explains
The same idea, in plain words
Explain it like I’m 10
A string is a row of letter tiles. In Python, the row might spell 'Python'. You can point at one tile: position 0 is P, and position -1 is the last tile, n. You can also take a strip of tiles. [1:4] means start at tile 1 and stop just before tile 4, so it gives yth.
The row is protected under clear plastic. You may read it, but you cannot pry out the first tile and swap in J. If you want 'Jython', you make a new row of tiles. Joining 'Ada' and ' Lovelace' is like placing two strips end to end. An f-string is a printed sentence with blank spaces: Python calculates what goes in the braces and fills it in.
Picture it like this
Strings are numbered rows of protected letter tiles, and slices are scissors placed between tiles.
Where the picture stops working
Python strings hold more than visible single-width letters, and a program stores values rather than physical tiles. The tile picture is mainly for order, positions, and slice boundaries.
Worked example
Trace the value of word = 'Python'. The positions are 0:P, 1:y, 2:t, 3:h, 4:o, and 5:n. word[1:4] starts at y and stops before position 4, so its value is 'yth'; word[-1] is 'n'. To create a label, 'Language: ' + word produces 'Language: Python'. For a message with a calculated value, f'{word[0]} is the first character' first evaluates word[0] as 'P', then produces 'P is the first character'. Trying word[0] = 'J' does not produce 'Jython': it raises TypeError because the existing string is immutable.
Key takeaway
Strings are immutable, ordered text sequences. Read them with zero-based indices or boundary-based slices, create new text with operations such as concatenation or f-strings, and trace the exact characters selected.
Quick check
3 questions here, of 5 in this lesson’s practice set. Answers stay hidden until you check.
Given word = 'Python', what does word[1:4] evaluate to?
Why does this statement fail? text = 'cat'; text[0] = 'b'
Study tools & related lessonsYou’ll learn to · Common mistakes · Easily confused · Key vocabulary · Related
You’ll learn to
- Define a Python string and recognize common string literal forms.
- Explain how zero-based, negative indexing and slicing select text.
- Distinguish an immutable string from a mutable sequence.
- Apply concatenation and a basic f-string to construct a message.
- Interpret common escape sequences and a Unicode escape.
Common mistakes
Treating the stop in text[a:b] as included.
Select from a through b - 1; the stop boundary is excluded.
Expecting text[0] = 'J' to edit the first character.
Strings are immutable; construct a new string instead.
Forgetting that the first index is 0.
Write a small position map before tracing an index or slice.
Expecting + to add a missing space between pieces of text.
Put the intended space in one of the string operands.
Easily confused
Indexing: text[2] vs. Slicing: text[2:5]
Indexing selects one character and fails if its position is out of range; slicing selects a substring between boundaries.
Ordinary string: '{name}' vs. f-string: f'{name}'
Only the f-string evaluates the expression in braces as part of creating its result.
Changing a name's binding vs. Changing a character in an existing string
A program can use a newly calculated string value, but it cannot mutate the original string's indexed character.
Key vocabulary
- string
- An ordered, immutable sequence of text characters in Python.
- string literal
- Source-code notation that creates a string value, usually by placing text between matching quotes.
- index
- An integer position used to select one item from a sequence.
- slice
- A selection of a sequence between boundaries, written with colon notation such as text[1:4].
- immutable
- Unable to have its existing contents altered in place.
- concatenation
- Creating one sequence by joining sequences end to end.
- escape sequence
- A backslash-led notation in a literal that represents a character or special action, such as \n for a newline.
- f-string
- A formatted string literal whose replacement fields in braces contain expressions.
Sources & references
- The Python Tutorial — 3. An Informal Introduction (Numbers) — Python Software Foundation
- The Python Language Reference — 2.7 Operators and delimiters — Python Software Foundation
EliExplains lessons are original prose written from the open, credible references above. See Copyright & Licensing.
Researched 2026-08-19
Educational content only. It is not medical, legal or professional advice. Found an error? Tell us.

