IN 30 SECONDS
A string looks like a word or a sentence, but to a computer, it is actually just an array of characters. Under the hood, each letter is stored as a number (like ASCII or Unicode). Because strings are basically arrays, all the tricks we learned for arrays (like indexing and pointers) apply here too! The biggest catch? In many languages, strings are immutable—once created, you cannot change a single letter without building a whole new string.
THE REAL-WORLD ANALOGY
Think of a string as a beaded necklace. Each bead represents a letter. If you want to know what the necklace says, you read the beads in order. Now, if the necklace is glued together (immutable), and you want to change one bead, you can't just pop it off. You have to thread an entirely new necklace, copying the beads you want to keep and inserting the new one.
PICTURE THIS
String: "CAT"\nIndices: 0 1 2\n [C] [A] [T]\nASCII: 67 65 84\n\nImmutability:\nWant to change "CAT" to "BAT"?\nOld String: [C][A][T] (Trashed)\n \\ \\ \\\nNew String: [B][A][T] (Newly allocated memory)
The first diagram shows how humans see letters, but the computer stores sequential ASCII numbers. The second shows how immutability forces us to allocate new memory for any change.
HOW IT ACTUALLY WORKS
- Encoding: When you type 'A', the computer consults an encoding table (like UTF-8) and stores the number 65.
- Storage: The characters are stored sequentially in memory, just like an array.
- Termination: Some languages (like C) use a special hidden character at the end (\0) so the computer knows where the word stops.
- Immutability (Python/Java): Modifying a string creates a completely new string in memory to prevent nasty bugs when sharing text data across a program.
SHOW ME THE CODE
def is_palindrome(s):\n # We clean the string first\n clean_s = s.lower()\n \n # We can use our Two Pointers trick here!\n left = 0\n right = len(clean_s) - 1\n \n while left < right:\n if clean_s[left] != clean_s[right]:\n return False # Mismatch found\n left += 1\n right -= 1\n \n return True # Read same forwards and backwards
DRY RUN
Input: "Radar"
| Step | State | Action | Match? |
|---|---|---|---|
| 1 | Cleaned: "radar" | L=0('r'), R=4('r') | Yes, move inward |
| 2 | "radar" | L=1('a'), R=3('a') | Yes, move inward |
| 3 | "radar" | L=2('d'), R=2('d') | Stop (L == R). Return True. |
SPEED & MEMORY
Checking or reading a string is fast. But because of immutability, concatenating strings in a loop is a performance disaster, as it copies the entire string over and over.
| Operation | Time | Space |
|---|---|---|
| Read char at index | O(1) | O(1) |
| String Concatenation | O(N) | O(N) (Creates new string) |
WHERE YOU'LL SEE IT IN REAL LIFE
- Search Autocomplete: Predicting what you type relies heavily on fast string prefix comparisons.
- DNA Sequencing: Genomic data is processed as massive strings of 'A', 'C', 'G', and 'T'.
TRAPS TO AVOID
- Trap 1: Using `+=` to build a string in a loop. Fix: Use an array/list to collect pieces, then `join()` them at the end.
- Trap 2: Forgetting case sensitivity. Fix: Always convert to `.lower()` or `.upper()` before comparing words.
- Trap 3: Ignoring whitespace. Fix: Remember to `.strip()` inputs in real-world applications.
PRACTICE
- Easy: Count the number of vowels in a given string.
- Medium: Check if two strings are anagrams of each other (contain the exact same characters, just scrambled).
TL;DR
- Strings are basically arrays of characters mapped to numerical encodings.
- In many modern languages, they are immutable (cannot be changed in-place).
- Avoid concatenating strings in a loop; build a list and join it instead.
REFERENCES CONSULTED
Referenced for verification — all content above is original. Reviewed fundamental definitions at GeeksforGeeks Strings.
COVERED LOG UPDATE
Added: Day 4: Strings