Strings: text in memory
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A string is how a program holds text: a row of characters in memory, each with a numbered place, just like a list. Follow a short Python program as it reads one character, slices out a few, counts them, and then tries to change one. It can't: strings never change, so +, upper() and join() each build a brand-new string beside the old one.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01With word = "café", what is word[1:3]?
A slice starts at the first index (1, "a") and stops just before the second (3), so it takes indexes 1 and 2: "af".
02What happens when you run word[0] = "C"?
Strings are immutable, so a character cannot be swapped in place. To get "Café", build a new string: "C" + word[1:].
03After new = word + "s", what does word hold?
+ builds a brand-new string in fresh memory and copies the characters into it. The old string, "café", is not touched.
04Why is "café" < "cat" True?
Python compares one character at a time. "c" and "a" match; the first difference is at index 2, and 102 is less than 116.
05You are joining 10,000 short pieces into one long text. What is the better way?
Each + builds a whole new string and copies everything so far, so the work grows fast. join() builds the result once.
المسرد
- String
- A value that holds text: a sequence of characters kept in order. In Python its type is str.
- Character
- One letter, digit, space or symbol in a string. In Python each character is one Unicode code point.
- Code point
- The number Unicode gives a character, such as 99 for c or 233 for é. ord() shows it.
- Index
- The numbered place of a character, counting from 0. Negative indexes count from the end (-1 is the last).
- Slice
- A run of characters, written s[start:stop]. It includes start and stops just before stop, and is a new string.
- Immutable
- Cannot be changed after it is made. Python strings are immutable, so changes always build a new string.
- Concatenation
- Joining strings end to end with +. The result is a new string.
- Method
- An action a value can do, written value.name(), such as word.upper() or text.split(",").
الملاحظات والمصادر
- The drawing gives each character one box. Inside, CPython stores a whole string as one block using 1, 2 or 4 bytes per code point, chosen by its widest character (PEP 393); "café" fits in 1 byte each.
- Here é is the single code point U+00E9. The same letter can also be written as e followed by a combining accent (U+0301); that string looks the same but has len 5.
- UTF-8 uses 1 byte for code points 0 to 127 (plain English letters and digits), 2 bytes up to U+07FF (such as é), and 3 or 4 bytes beyond that.
- The Python docs warn that building a string by repeated concatenation has a quadratic cost: doubling the text roughly quadruples the work. CPython can sometimes extend a string in place, but code should not rely on it.
- All capital letters A to Z (65 to 90) have smaller code points than the small letters a to z (97 to 122), so "Zoo" < "apple" is True.
- Python Library Reference: Text Sequence Type, str
- Python Library Reference: Common Sequence Operations (indexing, slicing, len, concatenation note)
- Python Library Reference: String Methods
- Python Language Reference: Value comparisons
- Python Language Reference: f-strings
- Python Tutorial: Text
- Python Unicode HOWTO
- RFC 3629: UTF-8, a transformation format of ISO 10646
- PEP 393: Flexible String Representation
اختبار يتاح بعد الخطوة الأخيرة.