Reading preferences
Concept

Strings

Treat text as an ordered sequence, while remembering that Python strings cannot be changed in place.

string, text, character, index, slice, immutable, unicode

Text is a sequence too

Write cat on three cards: c, a, t. Their order matters: act uses the same letters but says something different. A string is a text value, such as a name, word, or sentence.

Index012
Letter in catcat

For simple text like this, think of each position as one letter. Python strings are more precisely sequences of Unicode code points, the numbered units used to represent text.

Why strings are useful and where they are used

Software needs text for names, messages, file labels, and search words. Treating text as an ordered sequence lets us examine its parts, look for a letter, or copy a portion without inventing a variable for each position.

A string is not automatically a number: "12" is text, while 12 is a numeric value. Joining "1" and "2" gives "12"; adding 1 and 2 gives 3.

Read an index or copy a slice

word[0] reads the first position. word[-1] reads the last position in non-empty text. len(word) counts code points.

A slice copies a range: word[start:stop] includes start but excludes stop. For cat, [0:2] gives ca. A slice can safely produce an empty string; an individual index outside the string raises IndexError.

Strings cannot be changed in place

Python strings are immutable, meaning the existing string's positions cannot be replaced. word[0] = "C" would raise TypeError.

We can create "C" + word[1:] to make a new string and assign it to word. Changing what a variable refers to is not the same as changing the old string. This is different from replacing an item in a mutable Python list.

Python illustration

word = "cat"
print(word[1])
print(word[0:2])
new_word = "C" + word[1:]
print(new_word)
print(word)
print(word + "!")

Prepared output — not live execution:

a
ca
Cat
cat
cat!

The old text still says cat. Creating new_word or joining an exclamation mark does not edit the existing string.

Comparing and searching text

"cat" == "cat" asks whether two strings contain the same sequence. Uppercase and lowercase differ: "Cat" == "cat" is false.

Python's ordinary ordering compares code points lexicographically: it looks at the first differing position, not at the numeric meaning of the whole text. For example, "10" < "2" is true. This is not necessarily the alphabetical order expected for every language; human language sorting may require additional rules.

Costs and limitations

Reading one valid index is O(1) for Python strings. Creating a general slice or joining text can require time and storage proportional to the new text. Comparing long strings may need to inspect many positions.

One visible character can contain multiple code points, such as a letter with a combining mark or some emoji. Therefore len is not always the number of visible symbols, and reversing code points can break how text looks. Our small examples deliberately use simple Latin letters.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What does word[1] give when word is "cat"?
2. What does "cat"[0:2] contain?
3. How can you turn "cat" into "Cat" in Python?
4. What happens if you read index 0 of the empty string?

References and further reading